如何将字符串和整数组合成单个字符串 (C++)
How to combine strings and ints to a single string (C++)
可能的重复项:
C++连接字符串和整数
我正在尝试使用许多字符串和整数来制作单个字符串,但我收到消息:"错误 C2110:'+':无法添加两个指针"
这是我的代码:
transactions[0] = "New Account Made, Customer ID: " + ID + ", Customer Name : " + firstName + " " + secondName + endl + ", Account ID: " + setID + ", Account Name: " + setName;
(请注意,ID 和 setID 是一个整数)
使用字符串流:
#include <sstream>
...
std::stringstream stm;
stm<<"New Account Made, Customer ID: "<<ID<<", Customer Name : "<<firstName<<" "<<secondName<<std::endl<<", Account ID: "<<setID<<", Account Name: "<<setName;
然后,您可以使用 stm.str() 访问生成的字符串。
你应该使用字符串流:将字符串写入其中;然后写入 int。最后,通过流的str()
方法收获结果:
stringstream ss;
string hello("hello");
int world = 1234;
ss << hello << world << endl;
string res = ss.str();
cout << res << endl;
这是关于 ideone 的演示的链接。
相关文章:
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 如何将包含括号的字符串列表平展为单个列表?
- 将字符向量转换为单个字符串
- C++ 字符串单个字符
- 如何在 c++ 中将 2D 数组转换为单个字符串
- 将单个字符串/字符输入串行监视器
- 将字符串中的单个单词替换为另一个单词
- C++ 如何将字符串拆分为单个字符
- 将字符串拆分为单个单词并将它们放置在向量中
- 将C字符串转换为单个整数
- 查找单个字符串中重复次数最多和次数第二多的字符
- 字符串中单个字符作为数组的数据类型是什么
- 字符串中的重音,如何在单个字符中存储重音字符
- 从字符串中提取单个字符并将其转换为 int
- 为什么数组(字符串类型)的大小是 24 字节,带有单个空格元素
- 输入到字符串流并在单个语句中转换为字符串.可以没有帮助程序类
- 如何使用 deleteEntry 函数 c++ 删除数组中的单个字符串
- 反转具有单个循环的字符串
- spdlog 记录单个wchar_t字符串,其中包含包含 {..} 的数据
- 如何从空格分隔的单个字符串中提取多个子字符串