2016-08-02 92 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