2016-08-02 127 views
-4

如何连接i +名+字母+ i?如何在C++中连接字符串+ int +字符串?

for(int i = 0; i < 10; ++i){ 

    //I need a const char* to pass as a parameter to another function 
    const char* name = "mki"; 

    //The letter is equal to "A" for the first 2, "B" for the second 3, 
    //"C" for the following 4 ... 
    const char* final_string = ??? 
} 

我已经尝试使用:

std::to_string(i) 

但我得到一个错误,指出

to_string未定义性病

我使用Visual C++

+1

使用'的std :: stringstream' – Ari0nhh

+0

检查[这个答案](http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c/ 5590404#5590404)关于相关(不重复)的问题。 Sam的答案的宏观版本。 – DevSolar

回答

6

您有VC++不支持当前的C++标准的旧版本。在那种情况下,你必须以老式的方式来做。

#include <sstream> 

std::ostringstream o; 

o << "mki" << i << "abc"; 

std::string s=o.str(); 
-6

老式sprintf

char buf[10000]; 

sprintf(buf , "%s is %c string!!%d!!%d" , "this , 'a' , 1 ,1); 
+4

强烈建议不要使用此解决方案。这是一个C解决方案,而不是C++解决方案;并且容易发生缓冲区溢出(至少应该使用'snprintf')。 – mindriot