1. 程式人生 > >C++中string和int的相互轉化

C++中string和int的相互轉化

int轉化成string

string s = to_string(100);

int 轉化成char*

char s[128] = {0};

sprintf(s,"%d",100);

char *轉化成int

char * s = "1256" 

int a = atoi(s);

sscanf(s,"%d",&a);

string 轉化成int

#include<sstream>

int a; 

string s("123456");

stringstream iss(s);

iss>>a;

string 轉化成char*

string s ("123456");

char * p = s.c_str();