C++int类型转成string 类型

it2023-02-09  50

1.

int a = 10;

char *intStr = itoa(a);

string str = string(intStr);

2.

 

int a = 10;

stringstream ss;

ss << a;

string str = ss.str();

3. C++11  (推荐,如果支持的话)

 

#include <string>

 

std::string s = std::to_string(42);

4. C++ 98即可   (不支持C++11就用这个或者2,233 )

 

#include <sstream>

 

#define SSTR( x ) static_cast< std::ostringstream & >( \

( std::ostringstream() << std::dec << x ) ).str()

5. Boost

 

#include <boost/lexical_cast.hpp>

 

int num = 4;

std::string str = boost::lexical_cast<std::string>(num);

 

最新回复(0)