std::basic_ostringstream<CharT,Traits,Allocator>::str
提供: cppreference.com
< cpp | io | basic ostringstream
(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(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以上) |
ベースとなる文字列オブジェクトの内容を管理します。
1) ベースとなる文字列のコピーを返します。 return rdbuf()->str(); と同等です。
2)
a
をアロケータとして使用して、ベースとなる文字列のコピーを返します。 return rdbuf()->str(a); と同等です。3) ベースとなる文字列からムーブ構築した文字列を返します。 return std::move(*rdbuf()).str(); と同等です。
4-5) ベースとなる文字列の内容を置き換えます。 rdbuf()->str(s); と同等です。
6) ベースとなる文字列の内容を置き換えます。 rdbuf()->str(std::move(s)); と同等です。
目次 |
[編集]引数
s | - | ベースとなる文字列の新しい内容。 |
a | - | 返される文字列を構築するために使用するアロケータ。 |
[編集]戻り値
1-2) ベースとなる文字列オブジェクトのコピー。
3) ベースとなる文字列オブジェクトからムーブ構築された文字列。
4-6) (なし)
[編集]ノート
str
によって返されるベースとなる文字列のコピーは式の終わりで破棄される一時オブジェクトです。 そのため、 str() の結果に対して直接 c_str() を呼ぶと (例えば auto*ptr = out.str().c_str();)、 ダングリングポインタになります。
[編集]例
Run this code
#include <sstream>#include <iostream>int main(){int n; std::istringstream in;// could also use in("1 2") in.str("1 2"); in >> n;std::cout<<"after reading the first int from \"1 2\", the int is "<< n <<", str() = \""<< in.str()<<"\"\n"; 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); 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"
[編集]関連項目
紐付けられている文字列を置き換えまたはコピーを取得します ( std::basic_stringbuf<CharT,Traits,Allocator> のパブリックメンバ関数) |