在线文档教程
C++
输入/输出 | Input/output

std::basic_istringstream::str

性病:基本[医]StingStream::Str

std::basic_string str() const;(1)
void str(const std::basic_string<CharT,Traits,Allocator>& new_str(2)

管理基础字符串对象的内容。

1%29返回基础字符串的副本,就像通过调用rdbuf()->str()...

2%29替换基础字符串的内容,就像通过调用rdbuf()->str(new_str)...

参数

new_str-new contents of the underlying string

返回值

1%29基础字符串对象的副本。

2%29%28%29。

注记

返回的基础字符串的副本。str是一个临时对象,它将在表达式末尾被解构,因此直接调用c_str()关于…的结果str()%28,例如auto *ptr = out.str().c_str(%29导致一个悬空指针。

二次

#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"

二次

另见

strreplaces or obtains a copy of the associated character string (public member function of std::basic_stringbuf)

© cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

http://en.cppreference.com/w/cpp/io/basic[医]正流/str