2016-08-02 9 views
-4

i + name + letter + iをどのように連結できますか?C++でstring + int + stringをどのように連結できますか?

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++を使用していSTD

について定義されていないというエラーが発生しました。

+1

使用 'のstd :: stringstream' – Ari0nhh

+0

チェック[この回答](http://stackoverflow.com/questions/5590381/easiest-way-to-convert-int-to-string-in-c/ 5590404#5590404)を関連する(重複しない)質問に載せます。サムの答えのマクロ版。 – DevSolar

答えて

6

現在のC++標準をサポートしていないVC++の古いバージョンを持っています。その場合、あなたは古風なやり方でそれをしなければなりません。

#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

関連する問題