std::basic_istringstream<CharT,Traits,Allocator>::basic_istringstream - cppreference.com (original) (raw)
| (1) | ||
|---|---|---|
| explicit basic_istringstream( std::ios_base::openmode mode = std::ios_base::in ); | (until C++11) | |
| explicit basic_istringstream( std::ios_base::openmode mode ); | (since C++11) | |
| basic_istringstream() : basic_istringstream(std::ios_base::in) {} | (2) | (since C++11) |
| explicit basic_istringstream ( const std::basic_string<CharT, Traits, Allocator>& str, std::ios_base::openmode mode = std::ios_base::in ); | (3) | |
| explicit basic_istringstream ( std::basic_string<CharT, Traits, Allocator>&& str, std::ios_base::openmode mode = std::ios_base::in ); | (4) | (since C++20) |
| basic_istringstream( std::ios_base::openmode mode, const Allocator& a ); | (5) | (since C++20) |
| template< class SAlloc > basic_istringstream( const std::basic_string<CharT, Traits, SAlloc>& str, std::ios_base::openmode mode, const Allocator& a ); | (6) | (since C++20) |
| template< class SAlloc > basic_istringstream( const std::basic_string<CharT, Traits, SAlloc>& str, const Allocator& a ) : basic_istringstream(str, std::ios_base::in, a) {} | (7) | (since C++20) |
| template< class SAlloc > explicit basic_istringstream ( const std::basic_string<CharT, Traits, SAlloc>& str, std::ios_base::openmode mode = std::ios_base::in ); | (8) | (since C++20) |
| template< class StringViewLike > explicit basic_istringstream ( const StringViewLike& t, std::ios_base::openmode mode = std::ios_base::in ); | (9) | (since C++26) |
| template< class StringViewLike > basic_istringstream( const StringViewLike& t, std::ios_base::openmode mode, const Allocator& a ); | (10) | (since C++26) |
| template< class StringViewLike >basic_istringstream( const StringViewLike& t, const Allocator& a ); | (11) | (since C++26) |
| basic_istringstream( basic_istringstream&& other ); | (12) | (since C++11) |
Constructs new string stream.
Given
base_typeas std::basic_istream<CharT, Traits>, andbuf_typeas std::basic_stringbuf<CharT, Traits, Allocator>,
the std::basic_istream base and the exposition-only data member _sb_ are initialized as follows.
| Over load | std::basic_istream base | sb |
|---|---|---|
| (1) | base_type(std::addressof(sb))[1] | buf_type(mode | std::ios_base::in) |
| (2) | buf_type(std::ios_base::in) | |
| (3) | buf_type(str, mode | std::ios_base::in) | |
| (4) | buf_type(std::move(str), mode | std::ios_base::in) | |
| (5) | buf_type(mode | std::ios_base::in, a) | |
| (6) | buf_type(str, mode | std::ios_base::in, a) | |
| (7) | buf_type(str, std::ios_base::in, a) | |
| (8) | buf_type(str, mode | std::ios_base::in) | |
| (9) | std::addressof(sb) | {t, mode | std::ios_base::in, Allocator()} |
| (10) | {t, mode | std::ios_base::in, a} | |
| (11) | {t, std::ios_base::in, a} | |
| (12) | move constructed from other's std::basic_istream base | move constructed from other.sb |
- ↑ The std::basic_iostream base was intialized with base_type(&sb) (for overloads (1,3)) until C++11.
- This overload participates in overload resolution only if std::is_same_v<SAlloc, Allocator> is false.
[edit] Parameters
| str | - | string to use as initial contents of the string stream |
|---|---|---|
| t | - | an object (convertible to std::basic_string_view) to use as initial contents of the string stream |
| a | - | allocator used for allocating the contents of the string stream |
| mode | - | specifies stream open mode. It is a BitmaskType, the following constants are defined: Constant Explanation app seek to the end of stream before each write binary open in binary mode in open for reading out open for writing trunc discard the contents of the stream when opening ate seek to the end of stream immediately after open noreplace (C++23) open in exclusive mode |
| other | - | another string stream to use as source |
[edit] Notes
Construction of one-off basic_istringstream objects in a tight loop, such as when used for string conversion, may be significantly more costly than calling str() to reuse the same object.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
| __cpp_lib_sstream_from_string_view | 202306L | (C++26) | Interfacing std::stringstreams with std::string_view, (9-11) |
[edit] Example
Output:
buf1 = 7 n = 7 n = -10 test1
[edit] Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| P0935R0 | C++11 | the default constructor was explicit | made implicit |
[edit] See also
| | gets or sets the contents of underlying string device object (public member function) [edit] | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | constructs a basic_stringbuf object (public member function of std::basic_stringbuf<CharT,Traits,Allocator>) [edit] |