字符串与数字之间的转换

这篇文章发布已经超过两年,部分工具、版本或观点可能已经变化,请结合最新信息判断。

方法一(利用的stringstream,可以是浮点数)

数字to字符串

#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;
}

字符串to数字

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

int main()
{
    double x;
    string str;
    stringstream ss;
    cin >> str;
    ss << str;
    ss >> x;
    cout << x;
    return 0;
}
评论区已延迟加载滑到评论区附近时会自动加载,也可以现在手动打开。