首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

operators (std::basic_string)

Defined in header <string>

template <class CharT, class Traits, class Allocator> std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const std::basic_string<CharT, Traits, Allocator>& str);

(1)

template <class CharT, class Traits, class Allocator> std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, std::basic_string<CharT, Traits, Allocator>& str);

(2)

1%29表现为FormattedOutputFunction.在构造和检查哨兵对象之后,确定输出格式填充如下:

A%29如果str.size()不少于os.width(),使用范围[str.begin(), str.end())否则为b%29,则为(os.flags() & ios_base::adjustfield) == ios_base::left、地点os.width()-str.size()的副本os.fill()字符序列c%29之后的字符,否则,放置os.width()-str.size()的副本os.fill()字符序列之前的字符

然后存储结果序列%28中的每个字符str加上将%29填充到输出流os好像通过打电话os.rdbuf()->sputn(seq, n),在哪里n=std::max(os.width(), str.size())...

最后,呼叫os.width(0)取消…的影响std::setw如果有的话。

2%29表现为FormattedInputFunction.在构造和检查哨兵对象(可能跳过前导空格)之后,首先清除str带着str.erase(),然后从is并将它们附加到str好像str.append(1, c),直到下列条件之一变为真为止:

  • N字符被读取,其中Nis.width()如果is.width() > 0,否则Nstr.max_size()
  • 文件结束状态发生在流中。is
  • std::isspace(c,is.getloc())对于下一个字符是正确的。cis%28此空白字符保留在输入流%29中。

如果没有提取字符,那么std::ios::failbit被设定为is,这可能会std::ios_base::failure...

最后,呼叫os.width(0)取消…的影响std::setw如果有的话。

例外

1%5月29日投掷std::ios_base::failure如果在输出期间抛出异常。

2%5月29日投掷std::ios_base::failure如果没有从is%28E.。g流位于文件末尾,或仅由%29空格组成,或者在输入期间抛出异常。

参数

os

-

a character output stream

is

-

a character input stream

str

-

the string to be inserted or extracted

返回值

1%29os...

2%29is...

二次

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <sstream>
 
int main()
{
    std::string greeting = "Hello, whirled!";
    std::istringstream is(greeting);
    std::string hello_comma;
    is >> hello_comma;
    std::cout << greeting << '\n' << hello_comma << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
Hello, whirled!
Hello,

二次

代码语言:txt
复制
 © cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券