2012-06-27 174 views
0

可能重複:
Alternative to itoa() for converting integer to string C++?
How to convert a number to string and vice versa in C++
Append an int to a std::string如何整數轉換爲字符串

我想整數轉換爲字符串,任何一個可以幫助我的這些轉換?

itoa(*data->userid,buff1,10); 
itoa(*data->userphone,buff2,10); 
+3

對於下面發佈答案的所有人,不要再給另一個重複和追逐SO分數,您最好投票將其作爲副本關閉。 – mloskot

+1

三種可能性:http://stackoverflow.com/questions/10516196/append-an-int-to-a-stdstring/10516313#10516313 – hmjd

回答

8

對於C++,請改爲使用std::stringstream

#include <sstream> 

//... 
std::stringstream ss; 
ss << *data->userid; 
std::string userId = ss.str(); 

std::to_string如果您有權訪問C++ 11編譯器。

4

如果你有一個新的std::to_string函數的C++ 11編譯器,你可以使用它。否則,請使用Luchian的std::stringstream解決方案。