c_str()是C++ STL string类的成员函数,将string对象转化为char* 对象(包括最后的结束符’\0’),返回了一个指向字符数组的指针。
atoi()将char* 类型转化为整型
具体来说,atoi()函数会扫描char数组,跳过前边的空白字符,直到遇到正负符号之后开始进行转换,遇到非数字字符等其他字符时停止,返回结果。
简单示例:
#include <string>
#include <iostream>
using namespace std;
int main()
{
string str = "123";
int a = atoi(str.c_str());
cout<<a<<endl;
return 0;
}