C++中string和Int的相互转换

在算法竞赛中处理长数据的时候,我们往往采用string去存储一个数字,因为C++不像Java有BigInteger类,所以,string和Int的转换需要用到特殊的函数。只介绍最常用的方法。

Int->string(需要包含sstream头文件)

#include
#include
using namespace std;

int a = 1234;
stringstream ss;
ss<string str = ss.str();

只要短短几行代码就完成了类型的转换。


string->Int(需要包含stdlib.h头文件)

#include
#include
using namespacee std;
int main(){
string a = "1234";
int b = atoi(a.c_str());
cout<return 0;
}

你可能感兴趣的:(蓝桥杯,C++,C++语法)