我想将 int
转换为 char
或 char*
然后将它与 char*
连接起来>/p>
for (int i = 1; i < mymap.size(); i++)
{
char * s = "Scene";
cout << mymap[s+i];
}
我试过这种方式
string s = "Scene";
string x = to_string(i);
s = s + x;
char* si = s.c_str();
但是我得到一个错误。
但这行得通:
const char* si = s.c_str();
但后来我尝试将它与 mymap
一起使用:
cout << mymap[si];
我收到一条错误消息说我必须使用 char*
而不是 const char*
最佳答案
可能有点过分,但你可以试试 stringstream:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main(int argc, char * argv[]) {
char * a = "hello";
int b = 11;
stringstream sstm;
sstm << a << b;
string ans = sstm.str();
char * ans2 = (char *) ans.c_str();
cout << ans2 << endl; //"hello11"
return 0;
}
关于c++ - 转换 int 并将其与 char* 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17150182/