做字节的笔试的时候,突然有一步要用到,结果没想起来,炸了。
代码:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main()
{
string str="1234";
int num=4321;
cout<<"str转换前:"<<str<<endl;
stringstream s;
s<<num;
s>>str;
cout<<"str转换后:"<<str<<endl;
str="1234";
cout<<"num转换前:"<<num<<endl;
num=atoi(str.c_str());
cout<<"num转换后:"<<num<<endl;
system("pause");
return 0;
}
输出:
str转换前:1234
str转换后:4321
num转换前:4321
num转换后:1234