Files
cppdraft_translate/cppdraft/stringstream.md
2025-10-25 03:02:53 +03:00

315 lines
12 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[stringstream]
# 31 Input/output library [[input.output]](./#input.output)
## 31.8 String-based streams [[string.streams]](string.streams#stringstream)
### 31.8.5 Class template basic_stringstream [stringstream]
#### [31.8.5.1](#general) General [[stringstream.general]](stringstream.general)
[🔗](#lib:basic_stringstream)
namespace std {template<class charT, class traits = char_traits<charT>, class Allocator = allocator<charT>>class basic_stringstream : public basic_iostream<charT, traits> {public:using char_type = charT; using int_type = typename traits::int_type; using pos_type = typename traits::pos_type; using off_type = typename traits::off_type; using traits_type = traits; using allocator_type = Allocator; // [[stringstream.cons]](#cons "31.8.5.2Constructors"), constructors basic_stringstream() : basic_stringstream(ios_base::out | ios_base::in) {}explicit basic_stringstream(ios_base::openmode which); explicit basic_stringstream(const basic_string<charT, traits, Allocator>& s,
ios_base::openmode which = ios_base::out | ios_base::in);
basic_stringstream(ios_base::openmode which, const Allocator& a); explicit basic_stringstream( basic_string<charT, traits, Allocator>&& s,
ios_base::openmode which = ios_base::out | ios_base::in); template<class SAlloc> basic_stringstream(const basic_string<charT, traits, SAlloc>& s, const Allocator& a): basic_stringstream(s, ios_base::out | ios_base::in, a) {}template<class SAlloc> basic_stringstream(const basic_string<charT, traits, SAlloc>& s,
ios_base::openmode which, const Allocator& a); template<class SAlloc>explicit basic_stringstream(const basic_string<charT, traits, SAlloc>& s,
ios_base::openmode which = ios_base::out | ios_base::in); template<class T>explicit basic_stringstream(const T& t,
ios_base::openmode which = ios_base::out | ios_base::in); template<class T> basic_stringstream(const T& t, const Allocator& a); template<class T> basic_stringstream(const T& t, ios_base::openmode which, const Allocator& a);
basic_stringstream(const basic_stringstream&) = delete;
basic_stringstream(basic_stringstream&& rhs);
basic_stringstream& operator=(const basic_stringstream&) = delete;
basic_stringstream& operator=(basic_stringstream&& rhs); // [[stringstream.swap]](#swap "31.8.5.3Swap"), swapvoid swap(basic_stringstream& rhs); // [[stringstream.members]](#members "31.8.5.4Member functions"), members basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
basic_string<charT, traits, Allocator> str() const &; template<class SAlloc> basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
basic_string<charT, traits, Allocator> str() &&;
basic_string_view<charT, traits> view() const noexcept; void str(const basic_string<charT, traits, Allocator>& s); template<class SAlloc>void str(const basic_string<charT, traits, SAlloc>& s); void str(basic_string<charT, traits, Allocator>&& s); template<class T>void str(const T& t); private: basic_stringbuf<charT, traits, Allocator> *sb*; // *exposition only*};}
[1](#general-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9697)
The
class templatebasic_stringstream<charT, traits> supports reading and writing from objects of classbasic_string<charT, traits, Allocator>[.](#general-1.sentence-1)
It uses abasic_stringbuf<charT, traits, Allocator> object to control the associated sequence[.](#general-1.sentence-2)
For the sake of exposition, the maintained data is presented here as
- [(1.1)](#general-1.1)
*sb*, the stringbuf object[.](#general-1.sentence-3)
#### [31.8.5.2](#cons) Constructors [[stringstream.cons]](stringstream.cons)
[🔗](#lib:basic_stringstream,constructor)
`explicit basic_stringstream(ios_base::openmode which);
`
[1](#cons-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9720)
*Effects*: Initializes the base class withbasic_iostream<charT, traits>(addressof(*sb*)) ([[iostream.cons]](iostream.cons "31.7.5.7.2Constructors"))
and*sb* withbasic_stringbuf<charT, traits, Allocator>(which)[.](#cons-1.sentence-1)
[🔗](#lib:basic_stringstream,constructor_)
`explicit basic_stringstream(
const basic_string<charT, traits, Allocator>& s,
ios_base::openmode which = ios_base::out | ios_base::in);
`
[2](#cons-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9738)
*Effects*: Initializes the base class withbasic_iostream<charT, traits>(addressof(*sb*)) ([[iostream.cons]](iostream.cons "31.7.5.7.2Constructors"))
and*sb* withbasic_stringbuf<charT, traits, Allocator>(s, which)[.](#cons-2.sentence-1)
[🔗](#lib:basic_stringstream,constructor__)
`basic_stringstream(ios_base::openmode which, const Allocator& a);
`
[3](#cons-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9754)
*Effects*: Initializes the base class withbasic_iostream<charT, traits>(addressof(*sb*)) ([[iostream.cons]](iostream.cons "31.7.5.7.2Constructors"))
and *sb* withbasic_stringbuf<charT, traits, Allocator>(which, a) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2Constructors"))[.](#cons-3.sentence-1)
[🔗](#lib:basic_stringstream,constructor___)
`explicit basic_stringstream(
basic_string<charT, traits, Allocator>&& s,
ios_base::openmode which = ios_base::out | ios_base::in);
`
[4](#cons-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9770)
*Effects*: Initializes the base class withbasic_iostream<charT, traits>(addressof(*sb*)) ([[iostream.cons]](iostream.cons "31.7.5.7.2Constructors"))
and *sb* withbasic_stringbuf<charT, traits, Allocator>(std::move(s), which) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2Constructors"))[.](#cons-4.sentence-1)
[🔗](#lib:basic_stringstream,constructor____)
`template<class SAlloc>
basic_stringstream(
const basic_string<charT, traits, SAlloc>& s,
ios_base::openmode which, const Allocator& a);
`
[5](#cons-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9787)
*Effects*: Initializes the base class withbasic_iostream<charT, traits>(addressof(*sb*)) ([[iostream.cons]](iostream.cons "31.7.5.7.2Constructors"))
and *sb* withbasic_stringbuf<charT, traits, Allocator>(s, which, a) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2Constructors"))[.](#cons-5.sentence-1)
[🔗](#lib:basic_stringstream,constructor_____)
`template<class SAlloc>
explicit basic_stringstream(
const basic_string<charT, traits, SAlloc>& s,
ios_base::openmode which = ios_base::out | ios_base::in);
`
[6](#cons-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9804)
*Constraints*: is_same_v<SAlloc, Allocator> is false[.](#cons-6.sentence-1)
[7](#cons-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9808)
*Effects*: Initializes the base class withbasic_iostream<charT, traits>(addressof(*sb*)) ([[iostream.cons]](iostream.cons "31.7.5.7.2Constructors"))
and *sb* withbasic_stringbuf<charT, traits, Allocator>(s, which) ([[stringbuf.cons]](stringbuf.cons "31.8.2.2Constructors"))[.](#cons-7.sentence-1)
[🔗](#lib:basic_stringstream,constructor______)
`template<class T>
explicit basic_stringstream(const T& t, ios_base::openmode which = ios_base::out | ios_base::in);
template<class T>
basic_stringstream(const T& t, const Allocator& a);
template<class T>
basic_stringstream(const T& t, ios_base::openmode which, const Allocator& a);
`
[8](#cons-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9827)
Let which be ios_base::out | ios_base::in for the overload with no parameter which, anda be Allocator() for the overload with no parameter a[.](#cons-8.sentence-1)
[9](#cons-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9832)
*Constraints*: is_convertible_v<const T&, basic_string_view<charT, traits>> is true[.](#cons-9.sentence-1)
[10](#cons-10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9837)
*Effects*: Initializes the base class with addressof(*sb*), and
direct-non-list-initializes *sb* with t, which, a[.](#cons-10.sentence-1)
[🔗](#lib:basic_stringstream,constructor_______)
`basic_stringstream(basic_stringstream&& rhs);
`
[11](#cons-11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9849)
*Effects*: Move constructs from the rvalue rhs[.](#cons-11.sentence-1)
This
is accomplished by move constructing the base class, and the containedbasic_stringbuf[.](#cons-11.sentence-2)
Then calls basic_istream<charT, traits>::set_rdbuf(addressof(*sb*)) to install the contained basic_stringbuf[.](#cons-11.sentence-3)
#### [31.8.5.3](#swap) Swap [[stringstream.swap]](stringstream.swap)
[🔗](#lib:swap,basic_stringstream)
`void swap(basic_stringstream& rhs);
`
[1](#swap-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9866)
*Effects*: Equivalent to:basic_iostream<charT,traits>::swap(rhs);*sb*.swap(rhs.*sb*);
[🔗](#lib:swap,basic_stringstream_)
`template<class charT, class traits, class Allocator>
void swap(basic_stringstream<charT, traits, Allocator>& x,
basic_stringstream<charT, traits, Allocator>& y);
`
[2](#swap-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9883)
*Effects*: Equivalent to x.swap(y)[.](#swap-2.sentence-1)
#### [31.8.5.4](#members) Member functions [[stringstream.members]](stringstream.members)
[🔗](#lib:rdbuf,basic_stringstream)
`basic_stringbuf<charT, traits, Allocator>* rdbuf() const;
`
[1](#members-1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9896)
*Returns*: const_cast<basic_stringbuf<charT, traits, Allocator>*>(addressof(*sb*))[.](#members-1.sentence-1)
[🔗](#lib:str,basic_stringstream)
`basic_string<charT, traits, Allocator> str() const &;
`
[2](#members-2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9907)
*Effects*: Equivalent to: return rdbuf()->str();
[🔗](#lib:str,basic_stringstream_)
`template<class SAlloc>
basic_string<charT,traits,SAlloc> str(const SAlloc& sa) const;
`
[3](#members-3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9919)
*Effects*: Equivalent to: return rdbuf()->str(sa);
[🔗](#lib:str,basic_stringstream__)
`basic_string<charT,traits,Allocator> str() &&;
`
[4](#members-4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9930)
*Effects*: Equivalent to: return std::move(*rdbuf()).str();
[🔗](#lib:view,basic_stringstream)
`basic_string_view<charT, traits> view() const noexcept;
`
[5](#members-5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9941)
*Effects*: Equivalent to: return rdbuf()->view();
[🔗](#lib:str,basic_stringstream___)
`void str(const basic_string<charT, traits, Allocator>& s);
`
[6](#members-6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9952)
*Effects*: Equivalent to: rdbuf()->str(s);
[🔗](#lib:str,basic_stringstream____)
`template<class SAlloc>
void str(const basic_string<charT, traits, SAlloc>& s);
`
[7](#members-7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9964)
*Effects*: Equivalent to: rdbuf()->str(s);
[🔗](#lib:str,basic_stringstream_____)
`void str(basic_string<charT, traits, Allocator>&& s);
`
[8](#members-8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9975)
*Effects*: Equivalent to: rdbuf()->str(std::move(s));
[🔗](#lib:str,basic_stringstream______)
`template<class T>
void str(const T& t);
`
[9](#members-9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9987)
*Constraints*: is_convertible_v<const T&, basic_string_view<charT, traits>> is true[.](#members-9.sentence-1)
[10](#members-10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/iostreams.tex#L9992)
*Effects*: Equivalent to: rdbuf()->str(t);