C++int和string的相互转换

本方法主要利用sstream头文件中的方法来进行转换

1、int转成string

#include 
#include
#include
using namespace std;
int main()
{
    int i = 15;
    ostringstream o;
    o << i;
    string str = o.str();
    cout << str<

2、string转成int

#include 
#include
#include
using namespace std;
int main()
{
    string a = "18";
    stringstream ss(a);
    int b;
    ss >> b;
    cout << b;
}

你可能感兴趣的:(C++int和string的相互转换)