cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : IOstream Library : stringstream : stringstream
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
IOstream Library
manipulators
classes:
· filebuf
· fstream
· ifstream
· ios
· iostream
· ios_base
· istream
· istringstream
· ofstream
· ostream
· ostringstream
· streambuf
· stringbuf
· stringstream
objects:
· cerr
· cin
· clog
· cout
types:
· fpos
· streamoff
· streampos
· streamsize
stringstream
· stringstream::stringstr...
member functions:
· stringstream::rdbuf
· stringstream::str

-

stringstream::stringstream constructor member
explicit stringstream ( openmode which = ios_base::out|ios_base::in );
explicit stringstream ( const string & str,
                        openmode which = ios_base::out|ios_base::in );

Construct an object and optionally initialize its content

Constructs an object of class stringstream including the initialization of the associated stringbuf object and the call to its base class's constructor with the initialized stringbuf object as constructor parameter.

Additionally, in case that the second constructor version is used, the stream's buffer is initialized with the content of the string object str as if a call to member str.

Parameters

which
Bitmask with the requested i/o mode for the string buffer. This is an object of type ios_base::openmode. It generally consists of a combination of one or more the following flags (which are member constants):
flag valueopening mode
app(append) Set the stream's position indicator to the end of the stream before each output operation.
ate(at end) Set the stream's position indicator to the end of the stream on opening.
binary(binary) Consider stream as binary rather than text.
in(input) Allow input operations on the stream.
out(output) Allow output operations on the stream.
trunc(truncate) Any current content is discarded, assuming a length of zero on opening.
str
string object to be copied as the initial value for the internal buffer string.

Return Value

none

Example

// using stringstream constructors.
#include <iostream>
#include <sstream>
using namespace std;

int main () {

  int val;
  string mystr;
  stringstream ss (stringstream::in | stringstream::out);

  ss << "120 42 377 6 5 2000";

  for (int n=0; n<6; n++)
  {
    ss >> val;
    cout << val*2 << endl;
  }

  return 0;
}

This example uses the constructor to declare a stringstream for both input and output operations. A string is inserted into the stringstream and then it is extracted as integer values.

Basic template member declaration

( basic_stringstream<charT,traits,Allocator> )
explicit basic_stringstream ( openmode which = ios_base::out|ios_base::in );
explicit basic_stringstream ( const basic_string<charT,traits,Allocator> * str,
                               openmode which = ios_base::out|ios_base::in );

See also

stringstream::str Get/set the associated string object (public member function)

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