首页 > 其他 > 详细

string 与 int double 的转化

时间:2019-06-07 18:07:32      阅读:113      评论:0      收藏:0      [点我收藏+]
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
    //string -> int float double
    string str = "088.123";
    cout << "string -> int float double" << endl;
    cout << atoi(str.c_str()) << endl;
    cout << atol(str.c_str()) << endl;
    cout << atof(str.c_str()) << endl;
    int a;
    float b;
    double c;
    istringstream iss(str);
    iss >> a >> b >> c;
    cout << a << " " << b << " " << c << endl;

    //int double float -> string
    a = 10;
    b = 10.123;
    c = 100.234;
    cout << "int double float -> string" << endl;
    cout << to_string(a) << " " << to_string(b) << " " << to_string(c) << endl;
    stringstream ss;
    ss << a << " " << b << " " << c;
    cout << ss.str() << endl;

    return 0;
}

string 与 int double 的转化

原文:https://www.cnblogs.com/narjaja/p/10988472.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!