std::basic_...gstream::str(3)
π std::basic_ostringstream::str π
okay so this is the C++ Standard Library manual, section 3, for std::basic_ostringstream::str. The original header had that dramatic truncated std::basic_...gstream::str(3) energy and a Libary typo, but weβre folding the manual name in right here instead of copying the line, periodt. π
π NAME π
std::basic_ostringstream::str - std::basic_ostringstream::str β yes, the NAME line literally repeats itself, and honestly? main character energy. This is the member function that manages the contents of the underlying string object. π«Ά
β¨ SYNOPSIS β¨
Hereβs the overload fam, with the original labels in comments so you can match them to the list below:
// (1) (until C++20)
std::basic_string<CharT, Traits, Allocator> str() const;
// (1) (since C++20)
std::basic_string<CharT, Traits, Allocator> str() const&;
// (2) (since C++20)
template< class SAlloc >
std::basic_string<CharT, Traits, SAlloc> str( const SAlloc& a ) const;
// (3) (since C++20)
std::basic_string<CharT, Traits, Allocator> str() &&;
// (4)
void str( const std::basic_string<CharT, Traits, Allocator>& s );
// (5) (since C++20)
template< class SAlloc >
void str( const std::basic_string<CharT, Traits, SAlloc>& s );
// (6) (since C++20)
void str( std::basic_string<CharT, Traits, Allocator>&& s );
// (7) (since C++26)
template< class StringViewLike >
void str( const StringViewLike& t );
okay so what does it actually do? It manages the contents of the underlying string object. Hereβs the overload-by-overload tea:
- 1) Returns a copy of the underlying string. Equivalent to
return rdbuf()->str();. - 2) Returns a copy of the underlying string, using
aas allocator. Equivalent toreturn rdbuf()->str(a);. - 3) Returns a string move-constructed from the underlying string. Equivalent to
return std::move(*rdbuf()).str();. - 4,5) Replaces the contents of the underlying string. Equivalent to
rdbuf()->str(s);. - 6) Replaces the contents of the underlying string. Equivalent to
rdbuf()->str(std::move(s));. - 7) Replaces the contents of the underlying string. Equivalent to
rdbuf()->str(t);. This overload participates in overload resolution only ifis_convertible_v<const T&, basic_string_view<charT, traits>>is true.
π§Έ PARAMETERS π§Έ
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.
π RETURN VALUE π
- 1,2) A copy of the underlying string object.
- 3) A string move-constructed from the underlying string object.
- 4-7) (none).
π§ NOTES π§
β οΈ The copy of the underlying string returned bystris 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. Do not do this, bestie. Itβs giving dangling pointer, and not the cute kind. π
π§ͺ FEATURE-TEST MACRO π§ͺ
- Feature-test macro:
__cpp_lib_sstream_from_string_viewβ Value:202306Lβ Std:(C++26)β Feature: Interfacingstd::stringstreamswithstd::string_view,(7).
πΈ EXAMPLE πΈ
Run this code, itβs a whole vibe:
// Run this code
#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"
π SEE ALSO π
rdbufβ returns the underlying raw string device object (public member function).strβ replaces or obtains a copy of the associated character string (public member function ofstd::basic_stringbuf<CharT,Traits,Allocator>).
source: cppreference.com β’ version/date: 2024.06.10 β mwah, signed off with π π π«Ά, bestie. Bye!
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms