std::basic_stringbuf<CharT,Traits,Allocator>::str
来自cppreference.com
< cpp | io | basic stringbuf
(1) | ||
std::basic_string<CharT, Traits, Allocator> str()const; | (C++20 前) | |
std::basic_string<CharT, Traits, Allocator> str()const&; | (C++20 起) | |
template<class SAlloc> std::basic_string<CharT, Traits, Allocator> str(const SAlloc& a )const; | (2) | (C++20 起) |
std::basic_string<CharT, Traits, Allocator> str()&&; | (3) | (C++20 起) |
void str(conststd::basic_string<CharT, Traits, Allocator>& s ); | (4) | |
template<class SAlloc> void str(conststd::basic_string<CharT, Traits, SAlloc>& s ); | (5) | (C++20 起) |
void str(std::basic_string<CharT, Traits, Allocator>&& s ); | (6) | (C++20 起) |
template<class StringViewLike > void str(const StringViewLike& t ); | (7) | (C++26 起) |
获取和设置底层字符串。
在以下描述中,buf 和 mode 是 *this 的仅用于阐述的数据成员。
1) 创建并返回保有此
std::basic_stringbuf
底层字符序列副本的 std::basic_string。对于仅输入流,返回的字符串含来自范围 [
eback(),
egptr())
的字符。对于输入/输出或仅输出流,含有 pbase() 到序列中末字符的字符,不考虑 egptr() 和 epptr()。- 为写入打开的缓冲区中的成员字符序列可以为了效率而进行过分配。此时只会返回已初始化的字符:从构造函数字符串参数获得的字符、最近对
str()
的设置器重载的字符串参数的字符或来自写入操作的字符。典型的使用过分配的实现维护一个高水位指针,以跟踪缓冲区已初始化部分的结尾,而此重载返回从 pbase() 到高水位指针的字符。
- 为写入打开的缓冲区中的成员字符序列可以为了效率而进行过分配。此时只会返回已初始化的字符:从构造函数字符串参数获得的字符、最近对
| (C++20 起) |
2) 同 (1),但用 a 构造返回的 std::basic_string。等价于 returnstd::basic_string<CharT, Traits, SAlloc>(view(), a);。
此重载只有在
SAlloc
满足分配器(Allocator) 的要求时才会参与重载决议。3) 如同以从 *this 的在 buf 中的底层字符序列移动构造来创建 std::basic_string 对象。可能需要首先调节 buf 以使之含有同 (1) 的内容。构造完成后设置 buf 为空并调用
init_buf_ptrs()
,然后返回 std::basic_string 对象。5) 同 (4),但 s 的分配器的类型不是
Allocator
。 此重载只有在
SAlloc
与 Allocator
不是同一类型时才会参与重载决议。7) 如同用 std::basic_string_view<CharT, Traits> sv = t; 将 t 隐式转换到字符串视图 sv ,然后如同用 buf = sv 替换底层字符序列,然后调用
init_buf_ptrs()
。 此重载只有在 std::is_convertible_v<const StringViewLike&,
std::basic_string_view<CharT, Traits>> 是 true 时才会参与重载决议。
std::basic_string_view<CharT, Traits>> 是 true 时才会参与重载决议。
目录 |
[编辑]参数
s | - | 保有替换字符序列的 std::basic_string 对象 |
t | - | 用于初始化缓冲区的对象(可转换到 std::basic_string_view) |
a | - | 返回的 std::basic_string 的所有内存分配所用的分配器 |
[编辑]返回值
1-3) 保有此缓冲的底层字符序列的 std::basic_string 对象。
4-7) (无)
[编辑]注解
通常会通过 std::basic_istringstream::str()、std::basic_ostringstream::str() 或 std::basic_stringstream::str() 访问此函数。
功能特性测试宏 | 值 | 标准 | 功能特性 |
---|---|---|---|
__cpp_lib_sstream_from_string_view | 202306L | (C++26) | 字符串流的 std::string_view 接口 |
[编辑]示例
运行此代码
#include <iostream>#include <sstream> int main(){int n; std::istringstream in;// 也可以用 in("1 2") in.rdbuf()->str("1 2");// 设置获取区 in >> n;std::cout<<"从 \"1 2\" 读取第一个 int 之后,int 是 "<< n <<",str() = \""<< in.rdbuf()->str()<<"\"\n";// 或 in.str() std::ostringstream out("1 2"); out <<3;std::cout<<"将 int '3' 写入输出流 \"1 2\" 之后"<<",str() = \""<< out.str()<<"\"\n"; std::ostringstream ate("1 2", std::ios_base::ate);// C++11 ate <<3;std::cout<<"将 int '3' 写入附加流 \"1 2\" 之后"<<",str() = \""<< ate.str()<<"\"\n";}
输出:
从 "1 2" 读取第一个 int 之后,int 是 1,str() = "1 2" 将 int '3' 写入输出流 "1 2" 之后,str() = "3 2" 将 int '3' 写入附加流 "1 2" 之后,str() = "1 23"
[编辑]缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 432 | C++98 | 1. 重载 (1) 没有指定底层字符序列的内容 2. 重载 (4) 没有指定如何初始化输入和输出序列 | 两者均已指定 |
LWG 562 | C++98 | 重载 (4) 在 bool(mode &std::ios_base::out)==true 时会设置 epptr() 到指向最后一个底层字符的下一位置 | epptr() 可以设置到更后面的位置 |
[编辑]参阅
获取或设置底层字符串设备对象的内容 ( std::basic_stringstream<CharT,Traits,Allocator> 的公开成员函数) | |
(C++20) | 获得底层字符序列上的视图 (公开成员函数) |