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
ostream& operator<< (ostream& os, string& str);
<string>

Insert string into stream

Inserts the string object str into the output stream os.

This function overloads the global operator<< to behave as described in ostream::operator<< but applied to string objects.

Parameters

os
ostream object on which the insertion operation is performed.
str
string object output by the operation.

Return Value

The same as parameter os.

Errors are signaled by modifying the internal state flags:

flagerror
eofbit-
failbitWhen used with a streambuf object as parameter, failbit is set on if no characters could be extracted from it.
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 member function ios::exceptions, an exception of type ios_base::failure is thrown.

If some error happens during the output operation, the stream's badbit flag is set, and if the appropriate flag has been set with ios::exceptions, an exception is thrown.

Example

// example on insertion
#include <iostream>
#include <string>
using namespace std;

int main () {
  int n;

  string str ("test string");

  cout << str << endl;

  return 0;
}

Basic template declaration

template<class charT, class traits, class Allocator>
  basic_ostream<charT,traits>&
    operator<<(basic_ostream<charT,traits>& os,
               basic_string<charT,traits,Allocator>& str );

See also

ostream::operator<< Insert data with format (public member function)
operator>> Extract string from istream (function)

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