std::basic_...gstream::str(3)
π std::basic_istringstream::str(3) β C++ Standard Libary, bestie edition π
okay so this is the manual page for std::basic_istringstream::str from the C++ Standard Libary (yes that's how the header spells it, i don't make the rules β¨). coming to you straight from cppreference.com, dated 2024.06.10. let's get into it π«Ά
πΈ NAME
std::basic_istringstream::str β literally the member that reads and rewrites the underlying string inside your string stream. the main character of this whole page, no cap π
β¨ Synopsis
so there are seven overloads and they all want your attention. here they are, exactly as written, because i would never do you dirty like that:
std::basic_string<CharT, Traits, Allocator> str() const;
(until C++20)
std::basic_string<CharT, Traits, Allocator> str() const&;
(since C++20)
(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 );
π¦ what it actually does
Manages the contents of the underlying string object. that's the whole job. but like, how β let me spell it out π
- Returns a copy of the underlying string. Equivalent to
return rdbuf()->str();. - Returns a copy of the underlying string, using
aas allocator. Equivalent to
return rdbuf()->str(a);
- Returns a string move-constructed from the underlying string. Equivalent to
return std::move(*rdbuf()).str();
,5.Replaces the contents of the underlying string. Equivalent tordbuf()->str(s);.- Replaces the contents of the underlying string. Equivalent to
rdbuf()->str(std::move(s));
- 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. (iykyk π§)
π 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 stringaβ allocator used to construct the returned string β¨
π Return value
,2.A copy of the underlying string object. π«Ά- A string move-constructed from the underlying string object. π«
-7.(none) β that's it, that's the whole return value, she gives nothing back π
π Notes
β οΈ bestie listen to me. 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. π like fr fr do not do that. that's undefined behaviour territory and she will not be nice about it.
π₯ Feature-test macro
__cpp_lib_sstream_from_string_viewβ value202306Lβ std(C++26)β feature: Interfacingstd::stringstreamswithstd::string_view, applies to overload(7)β
π Example
// 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"
look at that. three streams, three little reveals. the ate one ate, honestly π₯
π§ 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>)
and that's the page, bestie π¦ go forth and not dangle any pointers π
β http://cppreference.com, 2024.06.10 πΈ slay β¨
π¦ 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