整型与字符串之间的相互转化,以及字符串的一些操作

  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
", str, val);
15 
16     strcpy(str, "runoob.com");
17     val = atoi(str);
18     printf("字符串值 = %s, 整型值 = %d
", 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 

   

 

To get,you have to give.To give,you need learn to insist.If you really find it is hard for you,then you quit.But once you quit.Don't complain.
原文地址:https://www.cnblogs.com/hit-ycy/p/10957606.html