std::basic_...stringstream(3)
π¦ std::basic_...stringstream(3) π¦
okay bestie, this is the std::basic_...stringstream(3) page from the C++ Standard Libary manual β yes, βLibaryβ is literally in the header and we are not fixing it. Weβre doing string-stream constructors today, all twelve overloads, and yes itβs giving main-character energy. π
πΈ NAME πΈ
std::basic_istringstream::basic_istringstream β thatβs the constructor. The whole NAME is just the constructor. Iconic, honestly.
β¨ Synopsis β¨
Constructs new string stream. Periodt.
Given:
base_typeasstd::basic_istream<CharT, Traits>, andbuf_typeasstd::basic_stringbuf<CharT, Traits, Allocator>,
the std::basic_istream base and the exposition-only data member sb are initialized as follows.
π The overloads, all twelve of them π
(1) (until C++11)
explicit basic_istringstream( std::ios_base::openmode mode = std::ios_base::in );
(1) (since C++11)
explicit basic_istringstream( std::ios_base::openmode mode );
(2) (since C++11)
basic_istringstream()
: basic_istringstream(std::ios_base::in) {}
(3)
explicit basic_istringstream(
const std::basic_string<CharT, Traits, Allocator>& str,
std::ios_base::openmode mode = std::ios_base::in );
(4) (since C++20)
explicit basic_istringstream(
std::basic_string<CharT, Traits, Allocator>&& str,
std::ios_base::openmode mode = std::ios_base::in );
(5) (since C++20)
basic_istringstream( std::ios_base::openmode mode, const Allocator& a );
(6) (since C++20)
template< class SAlloc >
basic_istringstream(
const std::basic_string<CharT, Traits, SAlloc>& str,
std::ios_base::openmode mode, const Allocator& a );
(7) (since C++20)
template< class SAlloc >
basic_istringstream(
const std::basic_string<CharT, Traits, SAlloc>& str,
const Allocator& a )
: basic_istringstream(str, std::ios_base::in, a) {}
(8) (since C++20)
template< class SAlloc >
explicit basic_istringstream(
const std::basic_string<CharT, Traits, SAlloc>& str,
std::ios_base::openmode mode = std::ios_base::in );
(9) (since C++26)
template< class StringViewLike >
explicit basic_istringstream(
const StringViewLike& t,
std::ios_base::openmode mode = std::ios_base::in );
(10) (since C++26)
template< class StringViewLike >
basic_istringstream(
const StringViewLike& t,
std::ios_base::openmode mode, const Allocator& a );
(11) (since C++26)
template< class StringViewLike >
basic_istringstream( const StringViewLike& t, const Allocator& a );
(12) (since C++11)
basic_istringstream( basic_istringstream&& other );
π§ Init table, but make it a list π§
If I donβt mention the std::basic_istream base for an overload, the table left that cell blank β Iβm not skipping it, itβs literally blank.
- (1) β
sb:buf_type(mode | std::ios_base::in). - (2) β
sb:buf_type(std::ios_base::in). - (3) β
sb:buf_type(str, mode | std::ios_base::in). - (4) β
sb:buf_type(std::move(str), mode | std::ios_base::in). - (5) β base:
base_type(std::addressof(sb))^[1];sb:buf_type(mode | std::ios_base::in, a). - (6) β
sb:buf_type(str, mode | std::ios_base::in, a). - (7) β
sb:buf_type(str, std::ios_base::in, a). - (8) β
sb:buf_type(str, mode | std::ios_base::in). - (9) β base:
std::addressof(sb);sb:{t, mode | std::ios_base::in, Allocator()}. - (10) β
sb:{t, mode | std::ios_base::in, a}. - (11) β
sb:{t, std::ios_base::in, a}. - (12) β base:
move constructed from other's std::basic_istream base;sb:move constructed from other.sb.
1. β Thestd::basic_iostreambase was intialized withbase_type(&sb)(for overloads(1,3)) until C++11.
π Overload-resolution gatekeeping π
- 8) This overload participates in overload resolution only if
std::is_same_v<SAlloc, Allocator>is false. - 9-11) These overloads participate in overload resolution only if
std::is_convertible_v<const StringViewLike&, std::basic_string_view<CharT, Traits>>is true.
π Parameters π
strβ string to use as initial contents of the string stream.tβ an object (convertible tostd::basic_string_view) to use as initial contents of the string stream.aβ allocator used for allocating the contents of the string stream.modeβ specifies stream open mode. It is aBitmaskType, and these constants are defined:appβ seek to the end of stream before each writebinaryβ open in binary modeinβ open for readingoutβ open for writingtruncβ discard the contents of the stream when openingateβ seek to the end of stream immediately after opennoreplace(C++23) β open in exclusive mode
otherβ another string stream to use as source.
π Notes π
Construction of one-off basic_istringstream objects in a tight loop, such as when used for string conversion, may be significantly more costly than calling str() to reuse the same object. So donβt be out here making a fresh one every single loop if you can recycle, bestie. π«Ά
__cpp_lib_sstream_from_string_viewβ value202306L, std(C++26), feature: Interfacingstd::stringstreamswithstd::string_view,(9-11).
π Example π
// Run this code
#include <iostream>
#include <sstream>
int main()
{
// default constructor (input/output stream)
std::stringstream buf1;
buf1 << 7;
int n = 0;
buf1 >> n;
std::cout << "buf1 = " << buf1.str() << " n = " << n << '\n';
// input stream
std::istringstream inbuf("-10");
inbuf >> n;
std::cout << "n = " << n << '\n';
// output stream in append mode (C++11)
std::ostringstream buf2("test", std::ios_base::ate);
buf2 << '1';
std::cout << buf2.str() << '\n';
}
π€ Output:
buf1 = 7 n = 7
n = -10
test1
π§ Defect reports π§
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
P0935R0β applied toC++11β behavior as published: the default constructor wasexplicit; correct behavior: made implicit.
π See also π
strβ gets or sets the contents of underlying string device object (public member function).constructorβ constructs abasic_stringbufobject (public member function ofstd::basic_stringbuf<CharT,Traits,Allocator>).
cute sign-off: source/version is http://cppreference.com, date is 2024.06.10, and the footer title is std::basic_...stringstream(3). bye bestie, go construct responsibly πβ¨
π¦ 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