C++中int类型和string类型的相互转换

#include 
#include  // atoi
#include 
#include  // stringstream
using namespace std;

int main(){
	// string2int using function s.str() and atoi() in 
	string s = "123";
	int a = atoi(s.c_str()); 
	cout << "string2int: " << a << endl;
	/*
	string.c_str(): string -> const char*
	atoi(): const char* -> int
	atof(): char* -> float
	atol(): char* -> long int
	*/
	
	// int2string using stringstream in 
	a = 111;
	stringstream ss;
	ss << a;
	s = ss.str();
	cout << "int2string: " << s << endl;
} 

 

你可能感兴趣的:(菜鸡怼题)