向字符串中添加变量和文本
Adding variables and text to a string?
有没有办法让字符串抓取一组文本和一个变量?类似这样的东西:
std::string morning = "morning";
std::string str = "Good " [insert morning here] ", user!";
很明显,我想做一些比我刚才举的例子更复杂的事情,但我相信你能理解我想做什么
提前谢谢,伙计们!
您只需使用std::string::operator+
,类似于:
std::string str = "Good " + morning + ", user!";
另一种方法是使用std::ostringstream
:
std::string morning = "morning";
unsigned int user_id = 384;
std::ostringstream out_stream;
out_stream << "Good " << morning << ", user #" << user_id;
std::string str = out_stream.str();
不幸的是,ostream和operator+都不起作用。我最后使用了一些字符和push_back的东西。不管怎样,谢谢大家!
相关文章:
- 如何使用文件中的文本,例如变量
- C++将文本从.txt文件解析为变量
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 文本 0 和 0 作为变量如何使用函数 __builtin_clz 产生不同的行为?
- 在C++中,从构造函数中将字符串文本分配给成员const char*变量时会发生什么
- 为什么 mfc 中静态文本工具中的变量未定义
- c++ 添加具有写入文件错误的文本变量
- 将文本文件读入类变量 - c++
- C++文本宏中的字符串变量
- 如何从文本文件中读取一行并将其分为不同的变量
- 是否有一种方法可以从文本文件中读取并将单个数据存储为不同的变量
- c ofstream写入变量到文本文件中
- 尝试使用文本文件初始化多个结构变量
- 如何在C++的文本变量中添加变量值
- fstream 变量获取文本文件中的所有行
- 声明具有常量引用与常量变量的常量文本字符串
- SDL:制作静态文本变量
- 如何在 Cv::Mat 图像中放置一些文本(变量)
- opengl在屏幕菜单上打印文本变量
- 在编译的代码中包含一个大的文本变量是个好主意吗