std::basic_stringbuf<CharT,Traits,Allocator>::str
来自 cppreference.cn
< 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, SAlloc> str( const SAlloc& a ) const; |
(2) | (C++20 起) |
std::basic_string<CharT, Traits, Allocator> str() &&; |
(3) | (C++20 起) |
void str( const std::basic_string<CharT, Traits, Allocator>& s ); |
(4) | |
template<class SAlloc> void str( const std::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_string 对象,含有此
std::basic_stringbuf
的底层字符序列的副本。对于只输入流,返回的字符串包含来自范围 [
eback(),
egptr())
的字符。对于输入/输出或只输出流,返回的字符串包含从 pbase() 到序列中最后一个字符的所有字符,无论 egptr() 和 epptr() 如何。- 为写入而打开的缓冲区的成员字符序列,可以为了效率而过度分配。在这种情况下,仅返回已初始化的字符:这些字符是来自构造函数的字符串实参、最近一次调用
str()
的设置器重载的字符串实参或来自写入操作的字符。一种使用过度分配的典型实现,会维护一个高水位标记(high-watermark)指针来跟踪缓冲区已初始化部分的末尾,而此重载返回从 pbase() 到高水位标记指针的字符。
- 为写入而打开的缓冲区的成员字符序列,可以为了效率而过度分配。在这种情况下,仅返回已初始化的字符:这些字符是来自构造函数的字符串实参、最近一次调用
|
(C++20 起) |
2) 同 (1),但用 a 构造返回的 std::basic_string。等价于 return std::basic_string<CharT, Traits, SAlloc>(view(), a);。
仅若
SAlloc
满足分配器(Allocator)的要求,此重载才会参与重载决议。3) 创建一个 std::basic_string 对象,如同从 *this 在 buf 中的底层字符序列移动构造它一样。可能需要首先调整 buf 以包含与 (1) 中相同的内容。之后,将 buf 置为空并调用
init_buf_ptrs()
,然后返回该 std::basic_string 对象。5) 同 (4),但 s 的分配器类型不是
Allocator
。 仅若 std::is_same_v<SAlloc, Allocator> 为 false,此重载才会参与重载决议。
7) 隐式转换 t 为一个字符串视图 sv,如同通过 std::basic_string_view<CharT, Traits> sv = t;,然后替换底层字符序列,如同通过 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; // could also use in("1 2") in.rdbuf()->str("1 2"); // set the get area in >> n; std::cout << "after reading the first int from \"1 2\", the int is " << n << ", str() = \"" << in.rdbuf()->str() << "\"\n"; // or in.str() std::ostringstream out("1 2"); out << 3; std::cout << "after writing the int '3' to output stream \"1 2\"" << ", str() = \"" << out.str() << "\"\n"; std::ostringstream ate("1 2", std::ios_base::ate); // C++11 ate << 3; std::cout << "after writing the int '3' to append stream \"1 2\"" << ", str() = \"" << ate.str() << "\"\n"; }
输出
after reading the first int from "1 2", the int is 1, str() = "1 2" after writing the int '3' to output stream "1 2", str() = "3 2" after writing the int '3' to append stream "1 2", str() = "1 23"
[编辑] 缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 432 | C++98 | 1. 重载 (1) 未指定底层 字符序列的内容 2. 重载 (4) 未指定如何初始化 输入和输出序列 |
均已指明 |
LWG 562 | C++98 | 若 bool(mode & std::ios_base::out) == true,重载 (4) 将 epptr() 设置为指向底层最后一个字符之后 的位置 |
epptr() 可以设置 到该位置之后 |
[编辑] 参阅
获取或设置底层字符串设备对象的内容 ( std::basic_stringstream<CharT,Traits,Allocator> 的公开成员函数) | |
(C++20) |
获取底层字符序列的视图 (公开成员函数) |