std::operator<<,>>
来自cppreference.com
< cpp | string | basic string
该页由英文版wiki使用Google Translate机器翻译而来。
该翻译可能存在错误或用词不当。鼠标停留在文本上可以看到原版本。你可以帮助我们修正错误或改进翻译。参见说明请点击这里. |
在头文件 <string> 中定义
|
||
template <class CharT, class Traits, class Allocator> std::basic_ostream<CharT, Traits>& |
(1) | |
template <class CharT, class Traits, class Allocator> std::basic_istream<CharT, Traits>& |
(2) | |
首先,构建一个std::basic_ostream::sentry对象,它刷新了tie()“D流,如果必要的,检查错误。如果哨兵对象返回false,立即退出.
原文:
First, constructs a std::basic_ostream::sentry object, which flushes the tie()'d stream if necessary and checks for errors. If the sentry object returns false, exits immediately.
此后,如果哨兵对象返回true,确定的输出格式中相同的方式作为第3阶段的num_put::put()填充.
原文:
Afterwards, if the sentry object returns true, determines the output format padding in the same manner as stage 3 of num_put::put().
然后将每个字符序列(
str
加填充的内容)到输出流os
,仿佛调用os.rdbuf()->sputn(seq, n),n=std::max(os.width(), str.size())原文:
Then stores each character from the resulting sequence (the contents of
str
plus padding) to the output stream os
as if by calling os.rdbuf()->sputn(seq, n), where n=std::max(os.width(), str.size())最后,调用os.width(0).
2) 首先,构建std::basic_istream::sentry对象,刷新了tie()'D如果有必要,提取物和丢弃所有前导空格字符流,除非ios_base::skipws标志被清除,并检查错误。如果哨兵对象返回false,立即退出.
原文:
First, constructs a std::basic_istream::sentry object, which flushes the tie()'d stream if necessary, extracts and discards all leading whitespace characters unless the ios_base::skipws flag was cleared, and checks for errors. If the sentry object returns false, exits immediately.
此后,如果哨兵对象返回true,清除
str
str.erase()原文:
Afterwards, if the sentry object returns true, clears
str
with str.erase()然后读取字符
is
并把它们添加str
如果由str.append(1, c)下列条件之一的,直到成为真正的:原文:
Then reads characters from
is
and appends them to str
as if by str.append(1, c), until one of the following conditions becomes true:-
N
个字符,其中N
is.width()
如果is.width() > 0
,否则N
是str.max_size()
原文:N
characters are read, whereN
isis.width()
ifis.width() > 0
, otherwiseN
isstr.max_size()
- 文件结束的条件发生在流
is
原文:the end-of-file condition occurs in the streamis
- std::isspace(c,is.getloc())是真正的下一个字符
c
is
(这个空白字符留在输入流中).原文:std::isspace(c,is.getloc()) is true for the next characterc
inis
(this whitespace character remains in the input stream).
如果没有字符被提取,然后std::ios::failbit上设置
is
,这可能会引发std::ios_base::failure.原文:
If no characters are extracted then std::ios::failbit is set on
is
, which may throw std::ios_base::failure.最后,调用os.width(0)和破坏std::basic_istream::sentry对象.
原文:
Finally, calls os.width(0) and destroys the std::basic_istream::sentry object.
目录 |
[编辑] 例外
1)如果抛出一个异常输出过程中,可能会引发std::ios_base::failure.
2) 原文:
may throw std::ios_base::failure if an exception is thrown during output.
可能会引发std::ios_base::failure乃摘录自
is
如果没有字符(如流位于文件末尾的空白,或由只),或在输入过程中,如果抛出一个异常.原文:
may throw std::ios_base::failure if no characters are extracted from
is
(e.g the stream is at end of file, or consists of whitespace only), or if an exception is thrown during input.[编辑] 参数
os | - | 字符输出流
|
is | - | 字符输入流
|
str | - | 字符串被插入或提取
原文: the string to be inserted or extracted |
[编辑] 返回值
1) os
2) is
[编辑] 示例
#include <iostream> #include <string> int main() { std::string greeting = "Hello, whirled!" std::cout << greeting << '\n'; }
输出:
Hello, whirled!