operator<<(std::basic_string_view)
来自cppreference.com
| 在标头 <string_view> 定义
|
||
template< class CharT, class Traits > std::basic_ostream<CharT, Traits>& operator<<( std::basic_ostream<CharT, Traits>& os, std::basic_string_view<CharT, Traits> v ); |
(C++17 起) | |
表现为有格式输出函数 (FormattedOutputFunction) 。构造并检查哨兵对象后,确定输出格式填充。
然后如同通过调用 os.rdbuf()->sputn(seq, n),将来自结果序列 seq(v 的内容加上填充)的每个字符存储到输出流 os,其中 n 是 std::max(os.width(), str.size())。
最后,调用 os.width(0) 以取消 std::setw 的效果,如果存在。
异常
如果输出时抛出异常,那么可能会抛出 std::ios_base::failure。
参数
| os | - | 字符输出流 |
| v | - | 要插入的视图 |
返回值
os
示例
运行此代码
#include <iomanip>
#include <iostream>
#include <string_view>
int main()
{
constexpr std::string_view s{"abc"};
constexpr int width{5};
// fill/left/right 属性保留不变直至被修改
std::cout << std::setfill('-');
std::cout << std::left;
std::cout << '[' << std::setw(width) << s << "]\n";
std::cout << '[' << std::setw(width) << s << "]\n";
std::cout << std::right;
std::cout << '[' << std::setw(width) << s << "]\n";
// 每次调用后重置宽度
std::cout << '[' << s << "]\n";
}
输出:
[abc--]
[abc--]
[--abc]
[abc]
参阅
| 执行字符串的流输入与输出 (函数模板) |