Skip to content

Instantly share code, notes, and snippets.

@DTSCode
Created December 2, 2014 01:49
Show Gist options
  • Save DTSCode/9ebbd5bcc598fca9e6d1 to your computer and use it in GitHub Desktop.
Save DTSCode/9ebbd5bcc598fca9e6d1 to your computer and use it in GitHub Desktop.
The basics of std::basic_stream<char>
The basics of std::basic_stream<char>
=====================================
As a part of the standard library, a class is defined called basic_stream<char>, which is typedef-ed to look like this:
typedef std::basic_stream<char> std::istream; // note that it might not look exactly like this, but it will evaluate to the same thing
The most known instance of std::istream is std::cin, which is linked to stdin, and currently all we are focusing on. There are two common operations used on std::cin. The first is operator >>, which reads one token from the input stream. There are two new terms here: token, and input stream. The input stream is literally just the collection of characters that are waiting to be read in to variables. In the case of std::cin, these characters typically come from the keyboard. For example, if I were to type "My name is DTSCode and I am an 3l1t3 h4xx0r. 10 20 30.4 -7", then that is what the input stream would consist of. The second term, token, is just a part of the input stream. Continuing with the example input stream from above, The tokens would be as such:
1: My
2: name
3: is
4: DTSCode
5: and
6: I
7: am
8: an
9: 3l1t3
10: h4xx0r.
11: 10
12: 20
13: 30.4
14: -7
So, std::cin >> some_string would first grab "My", then "name" would be the next token in the input stream (note: all of the quotes here are used to seperate the input stream from normal text. They don't actually exist in the input stream unless you type them in.). Now that we have the >> operator down, lets take a look at the second most common operation used on std::cin is std::cin.get(). std::cin.get() returns the next character from input. Still using the same input stream from above, the first three characters that would be returned are "M", "y", and " ". The difference between std::cin.get() and std::cin >> some_var is that get() returns the next character from input regardless of what it is, and operator >> grabs the next token from input and throws away whitespace. Ok, so now we have the two common ways of getting input from an input stream. Now, lets take a look at the two other kinds of streams in the standard library:
std::istringstream some_stream;
std::ifstream some_file_handle;
Let's take a look at the first one: std::istringstream some_stream. This is a stream class that looks like this:
namespace std {
class istringstream : istream {
};
}
Once again, it might not look exactly like this, but it will evaluate to the same thing. So now we know that istringstream inherits from istream (remember that std::cin is an std::istream). Not going into the mechanics of it, std::istringstream has operator >> and istringstream::get(), that work exactly the same way as the ones std::cin uses. The difference between istream and istringstream is that instead of waiting for keyboard input like std::cin does, you define the input stream yourself, like this: std::istringstream some_stream("The first lesson"). So some_stream >> some_variable; would make some_variable hold "The". some_variable = some_stream.get(); would make some_variable hold 'T'. The second type I listed, std::ifstream some_file_handle, works exactly the same way as the other two, except it gets its input from a file instead of a string (like istringstream) or the keyboard (like cin). Because of this, if you specify one of the parameters as std::istream some_stream, you can then pass an instance of std::istream (like std::cin), std::istringstream, or std::ifstream. Here is the final example:
void foo(std::istream some_stream) {
// do stuff with some_stream
}
void bar() {
std::istream a;
std::istringstream b;
std::ifstream c;
foo(a); foo(b); foo(c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment