共同点:
- 都是c++的字符处理函数,把数字字符串转变为int输出
- 头文件都是#include<cstring>
不同点:
- atoi()的参数是const char *,因此对于字异字符串str我们必须调用c_str()的方法把这个string 转换成const char *类型的,stoi()的参数是const string *,不需要转换成const char *;
- stoi()会做范围检查,默认范围是在int的范围内的,如果超出范围的话则会runtime error
#include "stdafx.h"
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
string s1 = "2147482", s2 = "-214748";
string s3 = "214748666666663", s4 = "-21474836488";
cout << stoi(s1) << endl;
cout << stoi(s2) << endl;
cout << atoi(s3.c_str()) << endl;
cout << atoi(s4.c_str()) << endl;
return 0;
}