一。數字--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);