命名空间
变体
操作

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 起)

获取和设置底层字符串。

在以下描述中,bufmode仅用于阐述的数据成员 *this

1) 创建并返回一个 std::basic_string 对象,其中包含此 std::basic_stringbuf 的底层字符序列的副本。对于仅输入流,返回的字符串包含来自范围 [eback()egptr()) 的字符。对于输入/输出或仅输出流,包含来自 pbase() 到序列中最后一个字符的字符,而与 egptr()epptr() 无关。
为提高效率,在打开以进行写入的缓冲区中的成员字符序列可以过度分配。在这种情况下,仅返回已初始化的字符:这些字符是从构造函数的字符串参数、最近一次调用 str() 的 setter 重载的字符串参数或写入操作获得的。使用过度分配的典型实现维护一个高水位线指针来跟踪缓冲区初始化部分的末尾,并且此重载返回从 pbase() 到高水位线指针的字符。
等效于 return std::basic_string<CharT, Traits, Allocator>(view(), get_allocator());
(自 C++20 起)
2)(1) 相同,除了 a 用于构造返回的 std::basic_string。等效于 return std::basic_string<CharT, Traits, SAlloc>(view(), a);
仅当 SAlloc 满足 Allocator 的要求时,此重载才参与重载解析。
3) 创建一个 std::basic_string 对象,就像通过从 *thisbuf 中的底层字符序列移动构造它一样。 buf 可能需要调整为首先包含与 (1) 中相同的内容。之后,将 buf 设置为空并调用 init_buf_ptrs(),然后返回 std::basic_string 对象。
4) 替换底层字符序列,就像通过 buf = s 一样,然后调用 init_buf_ptrs()
5)(4) 相同,除了 s 的分配器类型不是 Allocator
仅当 std::is_same_v<SAlloc, Allocator>false 时,此重载才参与重载解析。
6) 替换底层字符序列,就像通过 buf = std::move(s) 一样,然后调用 init_buf_ptrs()
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 时,此重载才参与重载解析。

目录

[编辑] 参数

s - a 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() 访问。

特性测试 Std 特性
__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++ 标准。

DR 应用于 已发布行为 正确行为
LWG 432 C++98 1. 重载 (1) 未指定内容
的底层字符序列
2. 重载 (4) 未指定如何初始化
输入和输出序列
均已指定
LWG 562 C++98 重载 (4) 设置 epptr() 以指向最后一个底层之后的位置
字符,如果 bool(mode & std::ios_base::out) == true
epptr() 可以设置为
超出该位置

[编辑] 参见

获取或设置底层字符串设备对象的内容
(std::basic_stringstream<CharT,Traits,Allocator> 的公有成员函数) [编辑]
(C++20)
获取底层字符序列的视图
(公有成员函数) [编辑]