C++ 参考手册
- C++11
- C++14
- C++17
- C++20
- C++ 编译器支持情况表
- 独立与宿主实现
- C++ 语言
- C++ 关键词
- 预处理器
- C++ 标准库头文件
- 具名要求
- 功能特性测试 (C++20)
- 工具库
- 类型支持(基本类型、RTTI、类型特性)
- 概念库 (C++20)
- 错误处理
- 动态内存管理
- 日期和时间工具
- 字符串库
- std::basic_string
- std::basic_string<CharT,Traits,Allocator>::npos
- std::basic_string<CharT,Traits,Allocator>::at
- std::hash (std::string, std::wstring, std::u16string, std::u32string, std::pmr::string, std::pmr::wstring, std::pmr::u16string, std::pmr::u32string)
- std::basic_string<CharT,Traits,Allocator>::assign
- std::basic_string<CharT,Traits,Allocator>::get_allocator
- std::basic_string<CharT,Traits,Allocator>::substr
- std::basic_string<CharT,Traits,Allocator>::basic_string
- std::basic_string<CharT,Traits,Allocator>::operator=
- std::basic_string<CharT,Traits,Allocator>::operator[]
- std::basic_string<CharT,Traits,Allocator>::front
- std::basic_string<CharT,Traits,Allocator>::back
- std::basic_string<CharT,Traits,Allocator>::data
- std::basic_string<CharT,Traits,Allocator>::c_str
- std::basic_string<CharT,Traits,Allocator>::operator basic_string_view
- std::basic_string<CharT,Traits,Allocator>::begin, std::basic_string<CharT,Traits,Allocator>::cbegin
- std::basic_string<CharT,Traits,Allocator>::end, std::basic_string<CharT,Traits,Allocator>::cend
- std::basic_string<CharT,Traits,Allocator>::rbegin, std::basic_string<CharT,Traits,Allocator>::crbegin
- std::basic_string<CharT,Traits,Allocator>::rend, std::basic_string<CharT,Traits,Allocator>::crend
- std::basic_string<CharT,Traits,Allocator>::empty
- std::basic_string<CharT,Traits,Allocator>::size, std::basic_string<CharT,Traits,Allocator>::length
- std::basic_string<CharT,Traits,Allocator>::max_size
- std::basic_string<CharT,Traits,Allocator>::reserve
- std::basic_string<CharT,Traits,Allocator>::capacity
- std::basic_string<CharT,Traits,Allocator>::shrink_to_fit
- std::basic_string<CharT,Traits,Allocator>::clear
- std::basic_string<CharT,Traits,Allocator>::insert
- std::basic_string<CharT,Traits,Allocator>::erase
- std::basic_string<CharT,Traits,Allocator>::push_back
- std::basic_string<CharT,Traits,Allocator>::pop_back
- std::basic_string<CharT,Traits,Allocator>::append
- std::basic_string<CharT,Traits,Allocator>::operator+=
- std::basic_string<CharT,Traits,Allocator>::compare
- std::basic_string<CharT,Traits,Allocator>::starts_with
- std::basic_string<CharT,Traits,Allocator>::ends_with
- std::basic_string<CharT,Traits,Allocator>::replace
- std::basic_string<CharT,Traits,Allocator>::copy
- std::basic_string<CharT,Traits,Allocator>::resize
- std::basic_string<CharT,Traits,Allocator>::swap
- std::basic_string<CharT,Traits,Allocator>::find
- std::basic_string<CharT,Traits,Allocator>::rfind
- std::basic_string<CharT,Traits,Allocator>::find_first_of
- std::basic_string<CharT,Traits,Allocator>::find_first_not_of
- std::basic_string<CharT,Traits,Allocator>::find_last_of
- std::basic_string<CharT,Traits,Allocator>::find_last_not_of
- std::operator+(std::basic_string)
- std::swap(std::basic_string)
- std::literals::string_literals::operator""s
- std::erase, std::erase_if (std::basic_string)
- operator<<,>>(std::basic_string)
- std::getline
- operator==,!=,<,<=,>,>=,<=>(std::basic_string)
- std::stoi, std::stol, std::stoll
- std::stoul, std::stoull
- std::stof, std::stod, std::stold
- std::to_string
- std::to_wstring
- std::basic_string 的推导指引
- std::basic_string_view
- 空终止字节字符串
- 空终止多字节字符串
- 空终止宽字符串
- std::char_traits
- 注释
- 容器库
- 迭代器库
- 范围库 (C++20)
- 算法库
- 数值库
- 输入/输出库
- 文件系统库
- 本地化库
- 正则表达式库
- 原子操作库
- 线程支持库
- 实验性 C++ 特性
- 有用的资源
- 索引
- std 符号索引
- 协程支持 (C++20)
- C++ 关键词
位置:首页 > C++ 参考手册 >字符串库 >std::basic_string > operator<<,>>(std::basic_string)
operator<<,>>(std::basic_string)
定义于头文件 <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) | |
1) 表现为有格式输出函数 (FormattedOutputFunction) 。构造并检查 sentry 对象后,以如下方式确定输出格式填充:
- a) 若
str.size()
不小于os.width()
,则原样地使用范围[str.begin(), str.end())
- b) 否则,若 (os.flags() & ios_base::adjustfield) == ios_base::left ,则放置
os.width()-str.size()
个os.fill()
字符的副本到字符序列后 - c) 否则,放置
os.width()-str.size()
个os.fill()
字符副本到字符序列前
然后存储每个来自结果序列( str
的内容加上填充)到输出流 os
,如同通过调用 os.rdbuf()->sputn(seq, n) ,其中 n=std::max(os.width(), str.size())
最后,调用 os.width(0) 取消 std::setw 的效果,若存在。
2) 表现为有格式输入函数 (FormattedInputFunction) 。构造并检查 sentry 对象,这可能跳过前导空白符,然后首先以 str.erase() 清除 str
,然后从 is
读取字符并后附它们到 str
,如同用 str.append(1, c) ,直至下列条件之一变为真:
- 读取了
N
个字符,其中若is.width() > 0
则N
为is.width()
,否则N
为str.max_size()
- 流
is
中出现文件尾条件 - std::isspace(c,is.getloc()) 对
is
中的下个字符c
为 true (空白符留在输入流中)。
若未释出字符,则设置 is
上的 std::ios::failbit ,这可能抛出 std::ios_base::failure 。
最后,调用 is.width(0) 取消 std::setw 的效果,若存在。
异常
1) 若输出中抛出异常则可能抛出 std::ios_base::failure 。
2) 若未从 is
释出字符(例如流在文件尾或仅有空白符组成),或若输入中抛出异常则可能抛出 std::ios_base::failure 。
参数
os | - | 字符输出流 |
is | - | 字符输入流 |
str | - | 插入或释出的字符串 |
返回值
1) os
2) is
示例
#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'; }
输出:
Hello, whirled! Hello,