Files
2025-10-25 03:02:53 +03:00

159 lines
5.2 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.

[string.io]
# 27 Strings library [[strings]](./#strings)
## 27.4 String classes [[string.classes]](string.classes#string.io)
### 27.4.4 Non-member functions [[string.nonmembers]](string.nonmembers#string.io)
#### 27.4.4.4 Inserters and extractors [string.io]
[🔗](#lib:operator%3e%3e,basic_string)
`template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
operator>>(basic_istream<charT, traits>& is, basic_string<charT, traits, Allocator>& str);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5016)
*Effects*: Behaves as a formatted input function ([[istream.formatted.reqmts]](istream.formatted.reqmts "31.7.5.3.1Common requirements"))[.](#1.sentence-1)
After constructing a sentry object,
if the sentry object returns true when converted to a value of type bool,
calls str.erase() and then extracts characters from is and appends them
to str as if by callingstr.append(1, c)[.](#1.sentence-2)
Ifis.width() is greater than zero, the maximum
number n of characters appended isis.width();
otherwise n isstr.max_size()[.](#1.sentence-3)
Characters are extracted and appended until any of the following
occurs:
- [(1.1)](#1.1)
*n* characters are stored;
- [(1.2)](#1.2)
end-of-file occurs on the input sequence;
- [(1.3)](#1.3)
isspace(c, is.getloc()) is true for the next available input character*c*[.](#1.sentence-4)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5047)
After the last character (if any) is extracted,is.width(0) is called and thesentry object is destroyed[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5054)
If the function extracts no characters,ios_base::failbit is set in the input function's local error state
before setstate is called[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5059)
*Returns*: is[.](#4.sentence-1)
[🔗](#lib:operator%3c%3c,basic_string)
`template<class charT, class traits, class Allocator>
basic_ostream<charT, traits>&
operator<<(basic_ostream<charT, traits>& os,
const basic_string<charT, traits, Allocator>& str);
`
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5073)
*Effects*: Equivalent to: return os << basic_string_view<charT, traits>(str);
[🔗](#lib:getline,basic_string)
`template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
basic_string<charT, traits, Allocator>& str,
charT delim);
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>&& is,
basic_string<charT, traits, Allocator>& str,
charT delim);
`
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5093)
*Effects*: Behaves as an unformatted input function ([[istream.unformatted]](istream.unformatted "31.7.5.4Unformatted input functions")),
except that it does not affect the value returned by subsequent calls tobasic_istream<>::gcount()[.](#6.sentence-1)
After constructing a sentry object,
if the sentry object returns true when converted to a value of type bool,
calls str.erase() and then extracts characters from is and appends them
to str as if by callingstr.append(1, c) until any of the following occurs:
- [(6.1)](#6.1)
end-of-file occurs on the input sequence;
- [(6.2)](#6.2)
traits::eq(c, delim) for the next available input character*c* (in which case,*c* is extracted but not appended);
- [(6.3)](#6.3)
str.max_size() characters are stored
(in which case,ios_base::failbit is set in the input function's local error state)[.](#6.sentence-2)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5123)
The conditions are tested in the order shown[.](#7.sentence-1)
In any case,
after the last character is extracted, thesentry object is destroyed[.](#7.sentence-2)
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5130)
If the function extracts no characters,ios_base::failbit is set in the input function's local error state
before setstate is called[.](#8.sentence-1)
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5135)
*Returns*: is[.](#9.sentence-1)
[🔗](#lib:getline,basic_string_)
`template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>& is,
basic_string<charT, traits, Allocator>& str);
template<class charT, class traits, class Allocator>
basic_istream<charT, traits>&
getline(basic_istream<charT, traits>&& is,
basic_string<charT, traits, Allocator>& str);
`
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/strings.tex#L5153)
*Returns*: getline(is, str, is.widen('\n'))[.](#10.sentence-1)