<string>
istream& operator>> (istream& is, string& str);
Extract string from istream
Extracts a string from the input stream is storing its content in str. Any previous content of str is cleared.
This function overloads the global operator>> to behave as described in istream::operator>> but applied to string objects.
Notice that the istream extraction operations use whitespaces as separators, therefore this operation will only extract what can be considered a word from the stream. To extract entire lines of text, refer to the string overload of global function getline.
Parameters
- is
- istream object on which the extraction operation is performed.
- str
- string object where the extracted content is stored.
Return Value
The same as parameter is.
Errors are signaled by modifying the internal state flags:
flag | error |
eofbit | The end of the source of characters is reached during its operations. |
failbit | The input obtained could not be interpreted as an element of the appropriate type.
Notice that some eofbit cases will also set failbit. |
badbit | An error other than the above happened. |
(see ios_base::iostate for more info on these)
Additionally, in any of these cases, if the appropriate flag has been set with is's member function ios::exceptions, an exception of type ios_base::failure is thrown.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
// example on extraction
#include <iostream>
#include <string>
using namespace std;
int main () {
int n;
string str;
cout << "Enter first name:";
cin >> str;
cout << "Thanks, " << str << ".\n";
return 0;
}
|
Basic template declaration
1 2 3 4
|
template<class charT, class traits, class Allocator>
basic_istream<charT,traits>&
operator>>(basic_istream<charT,traits>& is,
basic_string<charT,traits,Allocator>& str );
|
See also
getline | Get line from stream (function) |
|