| I/O manipulators | ||||
| Print functions(C++23) | ||||
| C-style I/O | ||||
| Buffers | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++20) | ||||
| Streams | ||||
| Abstractions | ||||
| File I/O | ||||
| String I/O | ||||
| Array I/O | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
(C++98/26*) | ||||
| Synchronized Output | ||||
(C++20) | ||||
| Types | ||||
| Error category interface | ||||
(C++11) | ||||
(C++11) |
| Member functions | ||||
(C++11) | ||||
| String operations | ||||
basic_ostringstream::str | ||||
(C++20) | ||||
| Non-member functions | ||||
| (1) | ||
std::basic_string<CharT, Traits, Allocator> str()const; | (until C++20) | |
std::basic_string<CharT, Traits, Allocator> str()const&; | (since C++20) | |
template<class SAlloc> std::basic_string<CharT, Traits, SAlloc> str(const SAlloc& a)const; | (2) | (since C++20) |
std::basic_string<CharT, Traits, Allocator> str()&&; | (3) | (since C++20) |
void str(conststd::basic_string<CharT, Traits, Allocator>& s); | (4) | |
template<class SAlloc> void str(conststd::basic_string<CharT, Traits, SAlloc>& s); | (5) | (since C++20) |
void str(std::basic_string<CharT, Traits, Allocator>&& s); | (6) | (since C++20) |
template<class StringViewLike> void str(const StringViewLike& t); | (7) | (since C++26) |
Manages the contents of the underlying string object.
Contents |
| s | - | new contents of the underlying string |
| t | - | an object (convertible tostd::basic_string_view) to use as the new contents of the underlying string |
| a | - | allocator used to construct the returned string |
The copy of the underlying string returned bystr is a temporary object that will be destructed at the end of the expression, so directly callingc_str() on the result ofstr() (for example inauto*ptr= out.str().c_str();) results in a dangling pointer.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_sstream_from_string_view | 202306L | (C++26) | Interfacingstd::stringstreams withstd::string_view,(7) |
#include <iostream>#include <sstream> int main(){int n; std::istringstream in;// could also use in("1 2") in.str("1 2"); in>> n;std::cout<<"After reading the first int from\"1 2\", the int is "<< n<<", str() =\""<< in.str()<<"\"\n"; 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); 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"
| returns the underlying raw string device object (public member function)[edit] | |
| replaces or obtains a copy of the associated character string (public member function of std::basic_stringbuf<CharT,Traits,Allocator>)[edit] |