c++的atoi和stoi一些区别
c++的atoi和stoi一些区别
对c++标准库中字符串转化为int的两个函数atoi()和stoi()两个有所混乱,特地研究了一下。
stoi()
标准库的函数默认模板
int stoi (const string& str, size_t* idx = 0, int base = 10);
int stoi (const wstring& str, size_t* idx = 0, int base = 10);
标准库中函数的解释
Parses str interpreting its content as an integral number of the specified base, which is returned as an int value.
If idx is not a null pointer, the function also sets the value of idx to the position of the first character in str after the number.
意思是解析str的文本转化为为int整数。
idx如果不为空,则会返回一个字符串中遇到的第一个字符(非数字)的字符下标,最后一个base是默认10进制。
example代码:
// stoi example
#include <iostream> // std::cout
#include <string> // std::string, std::stoi
int main ()
{
std::string str_dec = "2001, A Space Odyssey";
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;
}
Output
2001, A Space Odyssey: 2001 and [, A Space Odyssey]
40c3: 16579
-10010110001: -1201
0x7f: 127
atoi()
标准库的函数默认模板
int atoi (const char * str);
标准库中函数的解释
Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.
The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.
意思大概是解析字符串返回int。
首先会尽可能丢弃空格,直到找到一个第一个非空格字符,然后从这里开始,采用可选的初始加号和减号尽可能的10个数字来转化。
字符串中包含数字外其他字符会被自动忽略。
只包含空格或者不是有效整数会返回空格。
注意
这个函数是c中传到c++的,不会抛出异常,如果数组超出范围,可能会导致未定义的行为。
总结
- stoi用来转哈string的,atoi转化的是char[].
- char[]转string可以直接赋值或者用一个循环
- string转char
- str.data()
- str.c_str()
- str.copy()
- 个人感觉stoi的能力还是要比atoi的能力要大一些的,stoi可能往往要比atoi要好用。
c++的atoi和stoi一些区别的更多相关文章
- atoi、stoi、strtoi区别
首先atoi和strtol都是c里面的函数,他们都可以将字符串转为int,它们的参数都是const char*,因此在用string时,必须调c_str()方法将其转为char*的字符串.或者atof ...
- 【C++】atoi与stoi
stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error.atoi函数则不做范围检查,若超过int范围,则显示-214 ...
- atoi和stoi
vs环境下:stoi函数默认要求输入的参数字符串是符合int范围的[-2147483648, 2147483647],否则会runtime error.atoi函数则不做范围检查,若超过int范围,则 ...
- atoi()和stoi()函数
C++的字符处理函数,把数字字符串转换成int输出 头文件都是#include<cstring> atoi()的参数是 const char* ,因此对于一个字符串str我们必须调用 c_ ...
- 利用c++ std::getline实现split
getline reads characters from an input stream and places them into a string: getline从输入流中读取字符, 并把它们转 ...
- Loadrunner中web_find和web_reg_find函数的使用与区别
总结一下Loadrunner中的检查点函数,主要介绍两个函数:web_find()和web_reg_find():这两个函数均用于内容的查找,但两者也有本质的区别,具体介绍如下:一.web_find( ...
- leetcode:String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- C字符串和C++中string的区别 &&&&C++中int型与string型互相转换
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...
- C字符串和C++中string的区别 &&&&C++中int型与string型互相转换
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作.以下是C字符串和C++中string的区别: C字符串 string对象(C++) 所需的头文件名称 ...
随机推荐
- ogg:Extract 进程遇长事务执行 Forcestop 引发的惨案
http://www.linuxidc.com/Linux/2015-04/115777.htm SQL> select t.addr,t.START_DATE from v$transacti ...
- 64_p10
python3-matplotlib-qt4-2.0.0-2.fc26.2.x86_64.rpm 05-Apr-2017 09:54 29438 python3-matplotlib-qt5-2.0. ...
- [node.js] async/await如何优雅处理异常?
node.js的世界,从callback开始,不会止于async. 所有人都在骂为什么不能完全进化,其实我感觉这就是老外的细心,为了承上.这也就是为什么async其实就是promise一样,假如不是一 ...
- python3.X和python2.7的区别
1.python3.X将thread模块修改为_thread
- [转载]hazard pointer
hazard pointer 转载自: http://hi.baidu.com/rodimus/item/f6539cc179894f2f47d5c0ef 这是用于解决多线程并发下内存的回收,一块内存 ...
- 转:mysql日志(Windows下开启Mysql慢查询、通用日志)
一.Windows下开启Mysql慢查询详解 //show variables like '%quer%';查询是否开启了慢查询!! 第一步:修改my.ini(mysql配置文件) 在my.ini中 ...
- “您查看的网页正在试图关闭窗口。是否关闭此窗口”的屏蔽方法(JavaScript)
原文:http://www.cnblogs.com/tigerhuolh/archive/2011/04/14/2015634.html 用JS代码关闭窗口时会提示“您查看的网页正在试图关闭窗口.是否 ...
- CTF中的EXP编写技巧 zio库的使用
zio库没有提供文档 这个是官方给出的一个例子程序 from zio import * io = zio('./buggy-server') # io = zio((pwn.server, 1337) ...
- appium---【Mac】Appium-Doctor提示WARN:“ ios_webkit_debug_proxy cannot be found”解决方案
“ ios_webkit_debug_proxy cannot be found”报错截图如下: 解决方案: 打开terminal终端,分别输入执行结束,再次运行appium-doctor即可看到运行 ...
- AC日记——#2057. 「TJOI / HEOI2016」游戏 LOJ
#2057. 「TJOI / HEOI2016」游戏 思路: 最大流: 代码: #include <cstdio> #include <cstring> #include &l ...