字符串/数字 转换

一、string类型转换int类型

...
std::string str;
int i = std::stoi(str);
...

同样, 可以使用 stol(long), stof(float), stod(double) 等.

二、int类型转换string类型

    int a = 520;
    float b = 5.20;
    string str = "dong";
    string res = str + to_string(a);
    cout << res << endl;
    res = str + to_string(b);
    res.resize(8);
    cout << res << endl;

dong520

dong5.20

 

posted @ 2024-07-27 11:41  ImreW  阅读(1)  评论(0编辑  收藏  举报