"operator >>"
There are actually many. That operator is a text-book example of
function overloading.
1 2 3 4
|
int age;
std::string name;
std::cin >> age;
std::cin >> name;
| |
What is common to these two operator >> is that both skip leading whitespace and both take as many characters of content as possible.
The difference is in what they accept as "content" and what they do with it.
Let say that stream contains:
(Something that we see as whitespace, number, and newline.)
If we read to string, the whitespace is removed from string first, then "word"
3.14
is stored into the string and only the newline remains in the stream.
If we read to int, the whitespace is removed from string first, then value
3
is stored into the int and the stream still contains:
So, the operator >> for int stops not only on whitespace, but also on any character that does not look like integer. (Note:
0xF3 is an integer.)
What happens if you try to read non-int into int? For example, the
The operator >> for int can't read the
.
and therefore can't read anything
and it will set the stream into failed state.
All read operations, both >> and getline(), will automatically fail when stream is in failed state. The clear() can clear that state.