在C++中转换int为等效的最简单方法是什么?string我知道两种方法.有没有更简单的方法?
(1)
int a = 10;
char *intStr = itoa(a);
string str = string(intStr);
Run Code Online (Sandbox Code Playgroud)
(2)
int a = 10;
stringstream ss;
ss << a;
string str = ss.str();
Run Code Online (Sandbox Code Playgroud) 我认为这很简单,但它存在一些困难.如果我有
std::string name = "John";
int age = 21;
Run Code Online (Sandbox Code Playgroud)
如何组合它们以获得单个字符串"John21"?
我需要在循环的每次迭代中形成一个字符串,其中包含循环索引i:
for(i=0;i<100;i++) {
// Shown in java-like code which I need working in c!
String prefix = "pre_";
String suffix = "_suff";
// This is the string I need formed:
// e.g. "pre_3_suff"
String result = prefix + i + suffix;
}
Run Code Online (Sandbox Code Playgroud)
我试图使用的各种组合strcat,并itoa没有运气.