std:: stringstream
Objects of this class use a string buffer that contains a sequence of characters. This sequence of characters can be accessed directly as a string object, using member str .
Characters can be inserted and/or extracted from the stream using any operation allowed on both input and output streams.
This is an instantiation of basic_stringstream with the following template parameters:
template parameter | definition | comments |
---|---|---|
charT | char | Aliased as member char_type |
traits | char_traits | Aliased as member traits_type |
Alloc | allocator | Aliased as member allocator_type |
Apart from the internal string buffer, objects of these classes keep a set of internal fields inherited from ios_base , ios and istream :
field | member functions | description | |
---|---|---|---|
Formatting | format flags | flags setf unsetf | A set of internal flags that affect how certain input/output operations are interpreted or generated. See member type fmtflags . |
field width | width | Width of the next formatted element to insert. | |
display precision | precision | Decimal precision for the next floating-point value inserted. | |
locale | getloc imbue | The locale object used by the function for formatted input/output operations affected by localization properties. | |
fill character | fill | Character to pad a formatted field up to the field width ( width ). | |
State | error state | rdstate setstate clear | The current error state of the stream. Individual values may be obtained by calling good , eof , fail and bad . See member type iostate . |
exception mask | exceptions | The state flags for which a failure exception is thrown. See member type iostate . | |
Other | callback stack | register_callback | Stack of pointers to functions that are called when certain events occur. |
extensible arrays | iword pword xalloc | Internal arrays to store objects of type long and void* . | |
tied stream | tie | Pointer to output stream that is flushed before each i/o operation on this stream. | |
stream buffer | rdbuf | Pointer to the associated streambuf object, which is charge of all input/output operations. | |
character count | gcount | Count of characters read by last unformatted input operation. |
Member types
The class declares the following member types:
member type | definition |
---|---|
char_type | char |
traits_type | char_traits |
allocator_type | allocator |
int_type | int |
pos_type | streampos |
off_type | streamoff |
These member types are inherited from its base classes istream , ostream and ios_base :
event Type to indicate event type (public member type) event_callback Event callback function type (public member type) failure Base class for stream exceptions (public member class) fmtflags Type for stream format flags (public member type) Init Initialize standard stream objects (public member class) iostate Type for stream state flags (public member type) openmode Type for stream opening mode flags (public member type) seekdir Type for stream seeking direction flag (public member type) sentry (istream) Prepare stream for input (public member class) sentry (ostream) Prepare stream for output (public member class)
Public member functions
(constructor) Construct object (public member function) str Get/set content (public member function) operator= Move assignment (public member function) swap Swap internals (public member function)
Public member functions inherited from istream
operator>> Extract formatted input (public member function) gcount Get character count (public member function) get Get characters (public member function) getline Get line (public member function) ignore Extract and discard characters (public member function) peek Peek next character (public member function) read Read block of data (public member function) readsome Read data available in buffer (public member function) putback Put character back (public member function) unget Unget character (public member function) tellg Get position in input sequence (public member function) seekg Set position in input sequence (public member function) sync Synchronize input buffer (public member function)
Public member functions inherited from ostream
operator Insert formatted output (public member function) put Put character (public member function) write Write block of data (public member function) tellp Get position in output sequence (public member function) seekp Set position in output sequence (public member function) flush Flush output stream buffer (public member function)
Public member functions inherited from ios
good Check whether state of stream is good (public member function) eof Check whether eofbit is set (public member function) fail Check whether either failbit or badbit is set (public member function) bad Check whether badbit is set (public member function) operator! Evaluate stream (not) (public member function) operator bool Evaluate stream (public member function) rdstate Get error state flags (public member function) setstate Set error state flag (public member function) clear Set error state flags (public member function) copyfmt Copy formatting information (public member function) fill Get/set fill character (public member function) exceptions Get/set exceptions mask (public member function) imbue Imbue locale (public member function) tie Get/set tied stream (public member function) rdbuf Get/set stream buffer (public member function) narrow Narrow character (public member function) widen Widen character (public member function)
Public member functions inherited from ios_base
flags Get/set format flags (public member function) setf Set specific format flags (public member function) unsetf Clear specific format flags (public member function) precision Get/Set floating-point decimal precision (public member function) width Get/set field width (public member function) imbue Imbue locale (public member function) getloc Get current locale (public member function) xalloc Get new index for extensible array [static] (public static member function) iword Get integer element of extensible array (public member function) pword Get pointer element of extensible array (public member function) register_callback Register event callback function (public member function) sync_with_stdio Toggle synchronization with cstdio streams [static] (public static member function)
std:: stringstream::stringstream
(1) empty constructor (default constructor) Constructs a stringstream object with an empty sequence as content.
Internally, its iostream base constructor is passed a pointer to a stringbuf object constructed with which as argument. (2) initialization constructor Constructs a stringstream object with a copy of str as content.
Internally, its iostream base constructor is passed a pointer to a stringbuf object constructed with str and which as arguments. (3) copy constructor (deleted) Deleted (no copy constructor). (4) move constructor Acquires the contents of x .
First, the function move-constructs both its base iostream class from x and a stringbuf object from x ‘s internal streambuf object, and then associates them by calling member set_rdbuf .
x is left in an unspecified but valid state.
It is unspecified whether the sequence controlled by the internal stringbuf object is the one in x before the call, or a copy of it. In any case, both objects have internal string buffers that use independent sequences after the call.
The internal stringbuf object has at least the same duration as the stringstream object.
Parameters
str A string object, whose content is copied. x A stringstream object, whose value is moved.
which Open mode: Access given by the internal stringbuf object to its internal sequence of characters. It is an object of member type openmode for which any combination of the following member values is significant:
member constant | stands for | access |
---|---|---|
ios_base::in | input | The sequence supports input operations. |
ios_base::out | output | The sequence supports output operations. |
Other values of type ios_base::openmode may also be specified, although whether they have an effect on stringstream objects depends on the library implementation.
member constant | stands for | access |
---|---|---|
ios_base::in | input | The sequence supports input operations. |
ios_base::out | output | The sequence supports output operations. |
ios_base::ate | at end | The writing position is at the end after construction, and after every time the content is reset with member str . |
Other values of type ios_base::openmode (such as ios_base::app ) may also be specified, although whether they have an effect on stringstream objects depends on the library implementation.
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// swapping ostringstream objects // std::string // std::cout // std::stringstream int main () < std::stringstream ss; ss ' ' int foo,bar; ss >> foo >> bar; std::cout "foo: " << foo '\n'; std::cout "bar: " << bar '\n'; return 0; >