C++数字与字符串相互转化

使用sstream中的stringstream转换

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

int main()
{
    double x;
    string str;
    stringstream ss;
    cin >> x;
    ss << x;
    ss >> str;
    cout << str;
    return 0;
}

字符串转数字也是一样的道理,

注意如果需要多次转换,需要先使用

ss.clear();

来清空字符串流的缓存,才能接着转换

原文地址:https://www.cnblogs.com/zhouqianwei/p/14255167.html