c++ - 为什么string stream operator<<会删除原来的值

标签 c++

谁能解释为什么下面的代码会产生“you!”而不是“你好!”。

int main() {
    std::stringstream ss("hello");
    ss << " you!";
    cout << ss.str() << endl;
}

最佳答案

openmode 必须设置 ate

这就是你要找的:

int main() {
    std::stringstream ss("hello", std::ios_base::out | std::ios_base::ate);
    ss << " you!";
    cout << ss.str() << endl;
}

关于c++ - 为什么string stream operator<<会删除原来的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34758339/

相关文章:

c++ - gdb 重新设置断点时出错(无法访问内存)

c++ - 异步 ReadDirectoryChangesW 调用阻止线程退出

c++ - 模板化数学函数应该采用值还是 const 引用?

c++ - 在子目录中创建文件但无法打开文件

c++ - C++中数组char的初始化

c++ - 我们可以从设备句柄中获取设备对象的详细信息/名称吗

c++ - boost.test 与 CppUnit

c++ - 用 unit64_t 左移 - 发出警告

c++ - 规则定义中的 AST 和运算符优先级

c++ - 将 int 转换为长指针