整型與字符串之間的相互轉化,以及字符串的一些操做

  1. atoi和strtol都是c裏面的函數,他們均可以將字符串轉爲int,它們的參數都是const char*,所以在用string時,必須調c_str()方法將其轉爲char*的字符串。
  2. 它們都從字符串開始尋找數字或者正負號或者小數點,而後遇到非法字符終止,不會報異常。
    html

  3. stoi傳遞的參數必須是數字,否則會報錯

        int atoi(const char *str);   atoi,在stdlib.hios

 

        做用: 把參數 str 所指向的字符串轉換爲一個整數(類型爲 int 型) 數組

        參數: str – 要轉換爲整數的字符串 函數

        返回值 :該函數返回轉換後的長整數,若是沒有執行有效的轉換,則返回零。post

      

long int strtol(const char *str, char **endptr, int base) ;  strtol,在 stdlib.h、url

做用: 把參數 str 所指向的字符串根據給定的 base 轉換爲一個長整數(類型爲 long int 型),base 必須介於 2 和 36(包含)之間,或者是特殊值 0。 spa

參數: str:要轉換爲長整數的字符串。 3d

           endptr:對類型爲 char* 的對象的引用,其值由函數設置爲 str 中數值後的下一個字符。若參數endptr不爲NULL,則會將遇到不合條件而終止的nptr中的字符指針由endptr返回;若參數endptr爲NULL,則會不返回非法字符串 指針

           base:基數,必須介於 2 和 36(包含)之間,或者是特殊值 0。 code

 返回值:  該函數返回轉換後的長整數,若是沒有執行有效的轉換,則返回一個零值。

 1 #include <iostream>
 2 using namespace std;
 3 #include <stdio.h>
 4 #include <stdlib.h>
 5 #include <string.h>
 6 
 7 int main()
 8 {
 9     int val;
10     char str[20];
11 
12     strcpy(str, "98993489");
13     val = atoi(str);
14     printf("字符串值 = %s, 整型值 = %d\n", str, val);
15 
16     strcpy(str, "runoob.com");
17     val = atoi(str);
18     printf("字符串值 = %s, 整型值 = %d\n", str, val);
19 
20     string strnum=" 232s3112";
21     int num1=atoi(strnum.c_str());
22     long int num2=strtol(strnum.c_str(),nullptr,10); /// 程序在最開始遇到空格跳過,而後遇到了字符's'終止,最後返回了232。
23     cout<<"atoi的結果爲:"<<num1<<endl;
24     cout<<"strtol的結果爲:"<<num2<<endl;
25     
26 
27     using namespace std;
28     string strnum2="0XDEADbeE";
29     int num3=atoi(strnum2.c_str());///strtol的第三個參數base的含義是當前字符串中的數字是什麼進制,而atoi則只能識別十進制的。
30     long int num4=strtol(strnum2.c_str(),nullptr,16);
31     cout<<"atoi的結果爲:"<<num3<<endl;
32     cout<<"strtol的結果爲:"<<num4<<endl;
33     return(0);
34 }

 

 

 1 #include<iostream>
 2 #include<string>
 3 #include<sstream>
 4 using namespace std;
 5 string itos1(int i) // 將int 轉換成string
 6 {
 7     stringstream s;
 8     s << i;
 9     return s.str();
10 }
11 string itos2(int i) // 將int 轉換成string
12 {
13     stringstream s;
14     string ss;
15     s << i;
16     s >> ss;
17     return ss;
18 }
19 const int Stoi(string i) // 將string 轉換成int
20 {
21     stringstream s;
22     int iInt = 0;
23     s << i;
24     s >>iInt;
25     return iInt;
26 }
27 int main()
28 {
29     int val;
30     char str[20];
31 
32     strcpy(str, "98993489");
33     val = atoi(str);
34     int i = 127;
35     string i2 = itos2(i);
36     string i1 = itos1(i);
37     string sd = "98993489";
38     int s1 = stoi(str);
39     int s2 = Stoi(sd);
40     const char* p = i1.c_str();
41     cout << "atoi 輸出爲:" << val<<endl;    
42     cout << "stoi(系統自帶) 輸出爲:" << s1<<endl;
43     cout << "Stoi(本身寫) 輸出爲:" << s2<<endl;
44     cout << "itos1 輸出爲:" << i1<<endl;
45     cout << "itos2 輸出爲:" << i2<<endl;
46 
47 }

atoi 輸出爲:98993489
stoi(系統自帶) 輸出爲:98993489
Stoi(本身寫) 輸出爲:98993489
itos1 輸出爲:127
itos2 輸出爲:127

  • string與char數組的對比

     

  • stringstream的使用
 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     string s;
 9     stringstream ss;
10     int n;
11 
12     cin >> n;//cin會自動換行
13     getline(cin, s);//讀取換行
14     for (int i = 0; i < n; i++)
15     {
16         getline(cin, s);
17         ss.clear();
18         ss.str(s);
19 
20         int sum = 0;
21 
22         while (1)
23         {
24             int a;
25 
26             ss >> a;
27             if(ss.fail())///用來檢測是否到達文件尾部
28                 break;
29             sum += a;
30         }
31         cout << sum << endl;
32     }
33 
34     return 0;
35 }

3
1 2 3
輸出的和:6
22 22 55 22 22 22
輸出的和:165
123 123 213
輸出的和:459

看來stringstream彷佛不打算主動釋放內存( 或許是爲了提升效率 ),但若是你要在程序中用同一個流,反覆讀寫大量的數據,將會形成大量的內存消耗,所以這時候,須要適時地清除一下緩衝 ( 用 stream.str("")  )。

另外不要企圖用  stream.str().resize(0) 或  stream.str().clear()  來清除緩衝,使用它們彷佛可讓stringstream的內存消耗不要增加得那麼快,但仍然不能達到清除stringstream緩衝的效果(作個實驗就知道了,內存的消耗還在緩慢的增加)

 1 #include <iostream>
 2 #include <sstream>
 3 using namespace std;
 4 
 5 int main()
 6 {
 7     std::stringstream stream;
 8     string str;
 9 
10     while(1)
11     {
12         //clear()這個名字讓不少人想固然地認爲它會清除流的內容。
13         //實際上它並不清空任何內容,它只是重置了流的狀態標誌。
14         stream.clear();
15         //去掉下面這行註釋,清空stringstream的緩衝,每次循環內存消耗將再也不增長。
16         /*stream.str("");*/
17         stream << "you see see you";
18         stream >> str;
19         // 去掉下面這行註釋,看看每次循環,你的內存消耗會增長多少
20         cout << "Size of stream = " << stream.str().length() << endl;
21     }
22 
23     return 0;
24 }
在沒有加上stream.str("");以前輸出爲:

Size of stream = 15
Size of stream = 30
Size of stream = 45
Size of stream = 60
Size of stream = 75
Size of stream = 90

加上stream.str("");以後輸出爲:

Size of stream = 15Size of stream = 15Size of stream = 15Size of stream = 15Size of stream = 15Size of stream = 15

相關文章
相關標籤/搜索