首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::basic_stringstream::str

std::basic_string<CharT,Traits,Allocator> 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导致一个悬空指针。

二次

代码语言:javascript
复制
#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";
}

二次

产出:

二次

代码语言:javascript
复制
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"

二次

另见

str

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

代码语言:txt
复制
 ? cppreference.com

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

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com