码迷,mamicode.com
首页 > 其他好文 > 详细

string 与 int double 的转化

时间:2019-06-07 16:41:58      阅读:82      评论:0      收藏:0      [点我收藏+]

标签:ble   string   pac   clu   ace   atoi   turn   转化   return   

#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 的转化

标签:ble   string   pac   clu   ace   atoi   turn   转化   return   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!