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


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

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

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

 

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

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

        返回值 :該函數返回轉換后的長整數,如果沒有執行有效的轉換,則返回零。

      

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

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

參數: str:要轉換為長整數的字符串。 

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

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

 返回值:  該函數返回轉換后的長整數,如果沒有執行有效的轉換,則返回一個零值。

 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 = 15
Size of stream = 15
Size of stream = 15
Size of stream = 15
Size of stream = 15
Size of stream = 15 

 

   

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM