std::basic_ostringstream<CharT,Traits,Allocator>::str (3) - Linux Manuals
std::basic_ostringstream<CharT,Traits,Allocator>::str: std::basic_ostringstream<CharT,Traits,Allocator>::str
Command to display std::basic_ostringstream<CharT,Traits,Allocator>::str
manual in Linux: $ man 3 std::basic_ostringstream<CharT,Traits,Allocator>::str
NAME
std::basic_ostringstream<CharT,Traits,Allocator>::str - std::basic_ostringstream<CharT,Traits,Allocator>::str
Synopsis
std::basic_string<CharT,Traits,Allocator> str() const; (1)
void str(const std::basic_string<CharT,Traits,Allocator>& new_str); (2)
Manages the contents of the underlying string object.
1) Returns a copy of the underlying string as if by calling rdbuf()->str().
2) Replaces the contents of the underlying string as if by calling rdbuf()->str(new_str).
Parameters
new_str - new contents of the underlying string
Return value
1) a copy of the underlying string object.
2) (none)
Notes
The copy of the underlying string returned by str is a temporary object that will be destructed at the end of the expression, so directly calling c_str() on the result of str() (for example in auto *ptr = out.str().c_str();) results in a dangling pointer.
Example
// Run this code
#include <sstream>
#include <iostream>
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"
See also
replaces or obtains a copy of the associated character string
str (public member function of std::basic_stringbuf<CharT,Traits,Allocator>)
Pages related to std::basic_ostringstream<CharT,Traits,Allocator>::str
- std::basic_ostringstream<CharT,Traits,Allocator>::basic_ostringstream (3) - std::basic_ostringstream<CharT,Traits,Allocator>::basic_ostringstream
- std::basic_ostringstream (3) - std::basic_ostringstream
- std::basic_ostringstream::operator= (3) - std::basic_ostringstream::operator=
- std::basic_ostringstream::rdbuf (3) - std::basic_ostringstream::rdbuf
- std::basic_ostringstream::swap (3) - std::basic_ostringstream::swap
- std::basic_ostream (3) - std::basic_ostream
- std::basic_ostream<CharT,Traits>::basic_ostream (3) - std::basic_ostream<CharT,Traits>::basic_ostream
- std::basic_ostream<CharT,Traits>::flush (3) - std::basic_ostream<CharT,Traits>::flush
- std::basic_ostream<CharT,Traits>::operator<< (3) - std::basic_ostream<CharT,Traits>::operator<<
- std::basic_ostream<CharT,Traits>::operator= (3) - std::basic_ostream<CharT,Traits>::operator=
- std::basic_ostream<CharT,Traits>::put (3) - std::basic_ostream<CharT,Traits>::put
- std::basic_ostream<CharT,Traits>::seekp (3) - std::basic_ostream<CharT,Traits>::seekp
- std::basic_ostream<CharT,Traits>::sentry (3) - std::basic_ostream<CharT,Traits>::sentry
- std::basic_ostream<CharT,Traits>::swap (3) - std::basic_ostream<CharT,Traits>::swap
- std::basic_ostream<CharT,Traits>::tellp (3) - std::basic_ostream<CharT,Traits>::tellp
- std::basic_ostream<CharT,Traits>::write (3) - std::basic_ostream<CharT,Traits>::write
- std::basic_ostream<CharT,Traits>::~basic_ostream (3) - std::basic_ostream<CharT,Traits>::~basic_ostream
- std::basic_ostream_sentry (3)
- std::basic_osyncstream (3) - std::basic_osyncstream