Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
abs
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

string::operator>> 

Illustrates how to use the string::operator>> Standard Template Library (STL) function in Visual C++.


template<class E, class TYPE, class A> inline
   basic_istream<E, TYPE>&
   operator>>(basic_istream<E, TYPE>& InStream,
   basic_string<E, TYPE, A>& String);

NoteNote

The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

The operator>> is used to populate a string with the contents of an input stream.

Security noteSecurity Note

This operator copies data from an input source to a variable. If the input is not verified, this could potentially lead to a buffer overrun. For more information, see Avoiding Buffer Overruns.

// string_operator_extract_sample.cpp
// compile with: /EHsc
//
// Illustrates how to use the operator>> to extract
// a string from an input stream, populating a string
// variable with the contents.
//
// Functions:
//
//    operator>>  Extracts a string from an input stream.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <string>
#include <iostream>

using namespace std ;

int main()
{
   string s1;
   cout << "Enter a word: ";
   cin >> s1;
   cout << "You entered: " << s1 << endl;
}

Input

test

Sample Output

Enter a word: test
You entered: test

Header: <string>

Community Additions

ADD
Show:
© 2015 Microsoft