將數字字符串轉化成整型數據
將數字字符串轉化成整型數據我們將用到兩個函數。
1. c_str
它的作用是將string對象轉化成char*,為什么要這樣
做呢,這就要說起另外一個函數了。
2.atoi (它是array to integer的縮寫)
它的作用是將數字字符串轉化成整型數據。但是要注意
atoi(const char * s), 這個是它的標準用法,如果是
atoi(string s)這個就不行了。
具體的用法是。
View Code
string str="-1234"; int num = atoi(str.c_str());
string str = "1234";
int Num = atoi(str.c_str());
此時的Num應該就是1234了。
但是要注意:
str中只能包含0~9這10個數字和+、-這兩種符號。
其它的任何符號都不能有,否則不能正確得到結果。
posted on 2012-03-31 10:00 More study needed. 閱讀(722) 評論(0) 收藏 舉報

浙公網安備 33010602011771號