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, 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) |
獲取底層字元序列的檢視 (公開成員函式) |