1.string to int
string str = "123";
int iteger = atoi(str.c_str());
- c_str()函數返回一個指向正規C字符串的指針, 內容與本string串相同.這是爲了與c語言兼容,在c語言中沒有string類型,故必須通過string類對象的成員函數c_str()把string 對象轉換成c中的字符串樣式。
- 把字符串轉換成整型數。ASCII to integer 的縮寫。
2. int to string
string str;
int n=5;
stringstream ss; //#include<sstream>
ss<<n;
ss>>str;
cout<<str<<endl;