如何在c++中向字符串中添加多个项目
How to add multiple items to string in c++?
我知道怎么用cout:
cout << "string" << 'c' << 33;
但是如何执行此操作以便将输出重定向到变量而不是直接到标准输出?
const char* string << "string" << 'c' << 33; //doesn't work
使用c++标准库中的std::stringstream
它的工作原理如下:
std::stringstream ss;
ss << "string" << 'c' << 33;
std::string str = ss.str();
const char* str_ansi_c = str.c_str();
请记住,当您使用c样式的str_ansi_c
时,str
仍然需要在作用域中
#include <sstream>
#include <iostream>
main()
{
std::stringstream ss;
ss << "string" << 'c' << 33;
std::string str = ss.str();
std::cout << str;
}
相关文章:
- QT通过C++添加映射QML项目
- Visual Studio 中的 CMake 项目:如何添加其他包含和库目录?
- 尝试将 boost::stacktrace 添加到 CMake 项目时出现构建错误
- 从文本文件在组合框中添加项目
- 如何在视觉c ++项目中添加箭头键作为修饰键Microsoft
- 如何将 Lua 添加到C++项目
- 将项目添加到队列时运行线程
- Qt - 为什么Visual Studio 2019在qml资源中添加10Mb文件后无法再编译我的项目?
- 添加项目后C find()函数不起作用
- 如何在向列表中添加项目的同时迭代列表
- 如何在Qt中添加项目时保持在同一滚动位置
- 使用 push_back() 在向量中添加项目,向量的大小会增加但无法从向量中读取值
- 从键盘向列表中添加项目
- C++添加项目
- Qt QGraphicsScene缓慢添加项目
- 如何通过鼠标单击在QGraphicsView中选择位置并添加项目
- 智能感知和代码建议在添加c++项目后无法在Visual Studio 2013中工作
- 添加项目到CTreectrl c++
- Qt QTreeWidget逐个添加项目
- 在c++ Builder XE8中显示/添加项目到某些组合框