字符串与整数互相转换

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int str2int(string s)//字符串转换为整数
{
int n;
stringstream ss(s);
ss>>n;

return n;
}

string int2str(int n)//整数转换为字符串
{
string s;
stringstream ss;

ss<<n;
ss>>s;

return s;
}

int main()
{
cout << "123"+int2str(123) << endl;

cout << 123+str2int("123") << endl;

return 0;
}

你可能感兴趣的:(字符串)