字符串和整数转化常用函数
来源:互联网 发布:阿里云 知乎 编辑:程序博客网 时间:2024/05/21 10:50
几个常用的函数
今天要搞明白以下几个函数的含义,以及大致实现:
atoi、stoi、strtol、to_string
atoi
atoi:(表示 ascii to integer)是把字符串转换成整型数(可以正负)的一个函数
atoi( ) 函数会扫描参数 nptr字符串,跳过前面的空白字符(例如空格,tab缩进等,可以通过isspace( )函数来检测),直到遇上数字或正负符号才开始做转换,而再遇到非数字或字符串结束时(‘\0’)才结束转换,并将结果返回。如果 nptr不能转换成 int 或者 nptr为空字符串,那么将返回 0
cout<<atoi("232s3221")<<endl; cout << atoi("-123") << endl; cout << atoi("-123.11") << endl; cout << atoi("2147483649") << endl; cout << atoi("-2147483649") << endl;
输出结果分别是:
232//遇到非法字符,直接略过-123-123//遇到小数点,也直接略过2147483647-2147483648
很明显,对于溢出的,取int的上下限。
另外,需要注意:atoi()输入要求是const char *,所以如果要转换一个字符串时,可以进行下面的操作:
string strnum = "232s3221"; const char *ch = strnum.c_str();//取string的c_str cout << atoi(ch) << endl;//atoi(strnum.c_str())
strtol
long int strtol(const char *nptr,char **endptr,int base);
strtol函数会将参数nptr字符串根据参数base来转换成长整型数。
参数base范围从2至36,或0。参数base代表采用的进制方式,如base值为10则采用10进制,若base值为16则采用16进制等。当base值为0时则是采用10进制做转换,但遇到如’0x’前置字符则会使用16进制做转换、遇到’0’前置字符而不是’0x’的时候会使用8进制做转换。
一开始strtol()会扫描参数nptr字符串,跳过前面的空格字符,直到遇上数字或正负符号才开始做转换,再遇到非数字或字符串结束时(‘\0’)结束转换,并将结果返回。若参数endptr不为NULL,则会将遇到不合条件而终止的nptr中的字符指针由endptr返回;若参数endptr为NULL,则会不返回非法字符串。
char *ptr;cout << strtol("3.1415926 This stopped it",&ptr,10) << endl;cout << ptr << endl;
3.1415926 This stopped it
strtol基本上和atoi功能类似。只是转化为long int。
using namespace std; string strnum="0XDEADbeE"; int num1=atoi(strnum.c_str()); long int num2=strtol(strnum.c_str(),nullptr,16); cout<<"atoi的结果为:"<<num1<<endl; cout<<"strtol的结果为:"<<num2<<endl; return 0;
atoi的结果为:0strtol的结果为:233495534
另外可以注意的是,如果转换失败,这两个函数不会报错,而是返回0。
stoi
// stoi example#include <iostream> // std::cout#include <string> // std::string, std::stoiint main (){ std::string str_dec = "200100a00"; std::string str_hex = "40c3"; std::string str_bin = "-10010110001"; std::string str_auto = "0x7f"; std::string::size_type sz; // alias of size_t int i_dec = std::stoi (str_dec,&sz); int i_hex = std::stoi (str_hex,nullptr,16); int i_bin = std::stoi (str_bin,nullptr,2); int i_auto = std::stoi (str_auto,nullptr,0); std::cout << str_dec << ": " << i_dec << " and [" << str_dec.substr(sz) << "]\n"; std::cout << str_hex << ": " << i_hex << '\n'; std::cout << str_bin << ": " << i_bin << '\n'; std::cout << str_auto << ": " << i_auto << '\n'; return 0;}
对于非法字符,stoi是不会转化的
200100a00: 200100 and [a00]40c3: 16579-10010110001: -12010x7f: 127
上述三个函数的实现:
(待补充。。。。)
字符串处理需要注意的地方
字符串结束是’\0’
string s = "abcd"; for (int i = 0; i <= s.length(); i++) cout << s[i] << endl; if (s[4] == '\0') cout << "Yes" << endl;
abcdYes请按任意键继续. . .
但是如果访问是是s[5]则会报错
数组越界了!
换成数组,原理一样!
char ch[] = "abcd"; for (int i = 0; i <= 4; i++) cout << ch[i] << endl; if (ch[4] == '\0') cout << "Yes" << endl;
abcdYes请按任意键继续. . .
但是对于数组来讲,
int num[5] = { 1,2,3,4,5 }; cout << num[4] << endl;//5 cout << num[6] << endl;//溢出,-858993460,但是不存在数组访问越界
怎么访问结束符?
结束字符以’\0’
length(),sizeof(),strlen()
首先说sizeof
char *str1 = "absde"; char str2[] = "absde"; char str3[8] = { 'a', }; char str4[] = "0123456789"; cout << sizeof(str1) << endl;//4 cout << sizeof(str2) << endl;//6 cout << sizeof(str3) << endl;//8 cout << sizeof(str4) << endl;//11
解析:
首先说明一点,char类型占一个字节,所以sizeof(char)是1,这点要理解str1是一个指针,只是指向了字符串"absde"而已。所以sizeof(str1)不是字符串占的空间也不是字符数组占的空间,而是一个字符型指针占的空间。所以sizeof(str1)=sizeof(char*)=4,在C/C++中一个指针占4个字节str2是一个字符型数组。C/C++规定,对于一个数组,返回这个数组占的总空间,所以sizeof(str2)取得的是字符串"absde"占的总空间。"absde"中,共有a b s d e \0六个字符,所以str2数组的长度是6,所以sizeof(str2)=6*sizeof(char)=6str3已经定义成了长度是8的数组,所以sizeof(str3)为8str4和str2类似,'0' '1' ... '9'加上'\0'共11个字符,所以ss占的空间是8总之,对于指针,sizeof操作符返回这个指针占的空间,一般是4个字节;而对于一个数组,sizeof返回这个数组所有元素占的总空间。char*与char[]容易混淆,一定要分清,而且char*="aaa"的写法现在不被提倡,应予以避免而strlen不区分是数组还是指针,就读到\0为止返回长度。而且strlen是不把\0计入字符串的长度的。
strlen()
还是上述测试例子
char *str1 = "absde"; char str2[] = "absde"; char str3[8] = { 'a', }; char str4[] = "0123456789"; cout << strlen(str1) << endl;//5 不包含'\0' cout << strlen(str2) << endl;//5 不包含'\0' cout << strlen(str3) << endl;//1 cout << strlen(str4) << endl;//10
二者区别:
strlen指字符的长度(肯定不包括’\0’)
sizeof指文件或者数据占的内存(字节)
length
其实就是一个string字符串的长度,不包括’\0’。
正则表达式是啥?
参考:http://www.cnblogs.com/wzxwhd/p/6030083.html#undefined
待看链接:
要看的:
http://blog.csdn.net/monkey_d_feilong/article/details/51702400
https://zhidao.baidu.com/question/371960392412207164.html
http://bbs.csdn.net/topics/360064248
http://blog.csdn.net/huang_xw/article/details/8760403
- 字符串和整数转化常用函数
- 整数转化为字符串函数
- 字符串和整数相互转化
- 整数转化为字符串和字符串转化为整数
- 字符串分割+日期和整数的转化
- 字符串分割+日期和整数的转化
- 字符串和整数之间的相互转化
- 整数和数组转化为字符串
- c++实现atoi()和itoa()函数(字符串和整数转化)
- c++实现atoi()和itoa()函数(字符串和整数转化)
- 整数转化为整数字符串
- sscanf(字符串转化为整数)和sprintf(整数转化为字符串)
- 整数转化为字符串
- 整数字符串转化
- 字符串转化为整数
- 字符串转化成整数
- 字符串转化为整数
- 字符串转化成整数
- Leetcode——93. Restore IP Addresses
- Spring Web MVC框架(四) 视图和视图解析
- 关于JavaWeb中Tomcat端口被占用的解决方法
- The Unique MST (次小生成树)
- 【PAT】1047. Student List for Course
- 字符串和整数转化常用函数
- 归并排序
- Thinking in SQL系列之一:棋盘摆放问题
- 1. Two Sum - 两数求和
- [洛谷1265] 公路修建 - prim
- SQL初学讲解之——MySQL及NavicatforMySQL的安装和使用(仅供初学者)
- 初学Android:Activity与Intent小结
- 【2016——-编程语言排行以及优缺点分析】
- jdbc之批处理