cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : Strings library : operator>>
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Strings library
char_traits
classes:
· string
global functions:
· getline
· operator+
· operator<<
· operator>>
· comparison operators
· swap

-

operator>> function
istream& operator>> (istream& is, string& str);
<string>

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:

flagerror
eofbitThe end of the source of characters is reached during its operations.
failbitThe input obtained could not be interpreted as an element of the appropriate type.
Notice that some eofbit cases will also set failbit.
badbitAn error other than the above happened.
(see ios_base::iostate for more info on these)

Additionaly, 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

// 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

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

istream::operator>> Extract formatted data (public member function)
getline Get line from stream (function)
operator<< Insert string into stream (function)

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us