template< class StringViewLike >
void str( const StringViewLike& t );
Gets and sets the underlying string.
In the descriptions below, buf and mode are exposition-only data members of *this.
1)Creates and returns a
std::basic_stringobject containing a copy of this
std::basic_stringbuf
's underlying character sequence. For input-only streams, the returned string contains the characters from the range
[
eback(),
egptr())
. For input/output or output-only streams, contains the characters from
pbase()to the last character in the sequence regardless of
egptr()and
epptr().
str()
, or from a write operation. A typical implementation that uses over-allocation maintains a high-watermark pointer to track the end of the initialized part of the buffer and this overload returns the characters from pbase() to the high-watermark pointer.
This overload participates in overload resolution only if
SAlloc
meets the requirements of
Allocator.
3)Creates a
std::basic_stringobject as if by move constructing it from
*this's underlying character sequence in
buf.
bufmay need to be adjusted to contain the same content as in
(1)at first. After that, sets
bufto empty and calls
init_buf_ptrs()
, then returns the
std::basic_stringobject.
4)Replaces the underlying character sequence as if by
buf = s, then calls
init_buf_ptrs()
.
5) Same as (4), except the type of s's allocator is not Allocator
.
This overload participates in overload resolution only if
std::is_same_v<SAlloc, Allocator>is
false.
6)Replaces the underlying character sequence as if by
buf = std::move(s), then calls
init_buf_ptrs()
.
7)Implicitly converts
tto a string view
svas if by
std::basic_string_view<CharT, Traits> sv = t;, then replaces the underlying character sequence as if by
buf = sv, then calls
init_buf_ptrs()
.
[edit] Parameters [edit] Return value4-7) (none)
[edit] NotesThis function is typically accessed through std::basic_istringstream::str(), std::basic_ostringstream::str(), or std::basic_stringstream::str().
[edit] Example#include <iostream> #include <sstream> int main() { int n; std::istringstream in; // could also use in("1 2") in.rdbuf()->str("1 2"); // set the get area in >> n; std::cout << "after reading the first int from \"1 2\", the int is " << n << ", str() = \"" << in.rdbuf()->str() << "\"\n"; // or in.str() std::ostringstream out("1 2"); out << 3; std::cout << "after writing the int '3' to output stream \"1 2\"" << ", str() = \"" << out.str() << "\"\n"; std::ostringstream ate("1 2", std::ios_base::ate); // C++11 ate << 3; std::cout << "after writing the int '3' to append stream \"1 2\"" << ", str() = \"" << ate.str() << "\"\n"; }
Output:
after reading the first int from "1 2", the int is 1, str() = "1 2" after writing the int '3' to output stream "1 2", str() = "3 2" after writing the int '3' to append stream "1 2", str() = "1 23"[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 LWG 432 C++98 1. overload (1) did not specify the contentstd::basic_stringstream<CharT,Traits,Allocator>
) [edit] obtains a view over the underlying character sequence
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4