atoi和strtol都是c里面的函数,他们都可以将字符串转为int,它们的参数都是const char*,因此在用string时,必须调c_str()方法将其转为char*的字符串。
它们都从字符串开始寻找数字或者正负号或者小数点,然后遇到非法字符终止,不会报异常。
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
2 using namespacestd;3 #include
4 #include
5 #include
6
7 intmain()8 {9 intval;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<
26
27 using namespacestd;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<
1 #include
2 #include
3 #include
4 using namespacestd;5 string itos1(int i) //将int 转换成string
6 {7 stringstream s;8 s <
12 {13 stringstream s;14 stringss;15 s <>ss;17 returnss;18 }19 const int Stoi(string i) //将string 转换成int
20 {21 stringstream s;22 int iInt = 0;23 s <>iInt;25 returniInt;26 }27 intmain()28 {29 intval;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<
47 }
atoi 输出为:98993489
stoi(系统自带) 输出为:98993489
Stoi(自己写) 输出为:98993489
itos1 输出为:127
itos2 输出为:127
string与char数组的对比
stringstream的使用
1 #include
2 #include
3 #include
4 using namespacestd;5
6 intmain()7 {8 strings;9 stringstream ss;10 intn;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 inta;25
26 ss >>a;27 if(ss.fail())///用来检测是否到达文件尾部28 break;29 sum +=a;30 }31 cout << sum <
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
2 #include
3 using namespacestd;4
5 intmain()6 {7 std::stringstream stream;8 stringstr;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() <
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