c++ string、int、char數組互相轉換(方法簡單)

 

 

一。數字--string

(1)數字轉string(c++11有的新方法)

例:

int i = 9;

string str = to_string(i);

 

(2)string轉數字

stoi();stol();stoul();stoll();stoull();stof()

函數原型:int stoi (const string& str, size_t* idx = 0, int base = 10);

例:

int a;

string str = "1010";

a = stoi(str, 0, 2);//從0位置開始取2個數字

a爲10

 

二。string -- char數組

(1)string轉char數組

char *p;

string str = "hello";

p = str.c_str();

(2)char數組轉string

直接轉

char a[10] = "hello";

string str(a);

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章