std::basic_stringbuf::str(3)
okay so bestie π welcome to the std::basic_stringbuf::str(3) page, straight out of the C++ Standard Libary β yes, that exact spelling, we're keeping it. This is the whole str situation for std::basic_stringbuf: getting the underlying string and setting it, with all the overloads and caveats. Buckle up π
πΈ NAME πΈ
std::basic_stringbuf::str β std::basic_stringbuf::str
β¨ Synopsis β¨
Here's the overload lineup, bestie β same signatures, same conditions, no cap:
- (1)
std::basic_string<CharT, Traits, Allocator> str() const;β (until C++20) - (1)
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 );
In the descriptions below, buf and mode are exposition-only data members of *this. iykyk π«Ά
This function gets and sets the underlying string. Literally its whole job.
- Creates and returns a
std::basic_stringobject containing a copy of thisstd::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 frompbase()to the last character in the sequence regardless ofegptr()andepptr().
The member character sequence in a buffer open for writing can be over-allocated for efficiency purposes. In that case, only the initialized characters are returned: these characters are the ones that were obtained from the string argument of the constructor, the string argument of the most recent call to a setter overload of 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.
Equivalent to return std::basic_string<CharT, Traits, Allocator>(view(), get_allocator());. (since C++20)
- Same as (1), except that
ais used to construct the returnedstd::basic_string. Equivalent toreturn std::basic_string<CharT, Traits, SAlloc>(view(), a);. This overload participates in overload resolution only ifSAllocmeets the requirements ofAllocator. - Creates a
std::basic_stringobject as if by move constructing it from*this's underlying character sequence inbuf.bufmay need to be adjusted to contain the same content as in (1) at first. After that, setsbufto empty and callsinit_buf_ptrs(), then returns thestd::basic_stringobject. - Replaces the underlying character sequence as if by
buf = s, then callsinit_buf_ptrs(). - Same as (4), except the type of
s's allocator is notAllocator. This overload participates in overload resolution only ifstd::is_same_v<SAlloc, Allocator>isfalse. - Replaces the underlying character sequence as if by
buf = std::move(s), then callsinit_buf_ptrs(). - Implicitly converts
tto a string viewsvas if bystd::basic_string_view<CharT, Traits> sv = t;, then replaces the underlying character sequence as if bybuf = sv, then callsinit_buf_ptrs().
This overload participates in overload resolution only if std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>> is true.
π Parameters π
sβ astd::basic_stringobject holding the replacement character sequencetβ an object (convertible tostd::basic_string_view) holding the replacement character sequenceaβ allocator to use for all memory allocations of the returnedstd::basic_string
π Return value π
- 1β3) A
std::basic_stringobject holding this buffer's underlying character sequence. - 4β7) (none)
π Notes π
This function is typically accessed through std::basic_istringstream::str(), std::basic_ostringstream::str(), or std::basic_stringstream::str().
Feature-test macro moment β:
__cpp_lib_sstream_from_string_viewβ value202306L, std(C++26), feature: Interfacing string streams withstd::string_view
π¦ Example π¦
// Run this code
#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"
π Defect reports π
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. Not me reading the lore π
- DR
LWG 432, applied toC++98β behavior as published: 1. overload (1) did not specify the content of the underlying character sequence; 2. overload (4) did not specify how the input and output sequences are initialized. Correct behavior: both specified. - DR
LWG 562, applied toC++98β behavior as published: overload (4) setepptr()to point one past the last underlying character ifbool(mode & std::ios_base::out) == true. Correct behavior:epptr()can be set beyond that position.
π See also π
strβ gets or sets the contents of underlying string device object; public member function ofstd::basic_stringstream<CharT,Traits,Allocator>view(C++20) β obtains a view over the underlying character sequence; public member function
source/version: http://cppreference.com Β· date: 2024.06.10 Β· page: std::basic_stringbuf::str(3) β byeee bestie, go slay those string streams π
β¨π«Ά
π¦ 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