zoukankan      html  css  js  c++  java
  • string类型转换int类型

    C++转换形式(C++11):

    int main(int argc, char* argv[])
    {    
        std::string str1 = "45";
        std::string str2 = "3.14159";
        std::string str3 = "31337 with words";
        std::string str4 = "words and 2";
    
        int myint1 = std::stoi(str1);
        int myint2 = std::stoi(str2);
        int myint3 = std::stoi(str3);
        // error: 'std::invalid_argument'
         /*int myint4 = std::stoi(str4);*/
    
        std::cout << "std::stoi("" << str1 << "") is " << myint1 << '
    ';
        std::cout << "std::stoi("" << str2 << "") is " << myint2 << '
    ';
        std::cout << "std::stoi("" << str3 << "") is " << myint3 << '
    ';
        /*std::cout << "std::stoi("" << str4 << "") is " << myint4 << '
    ';*/
    }

    output:

    std::stoi("45") is 45
    std::stoi("3.14159") is 3
    std::stoi("31337 with words") is 31337

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

  • 相关阅读:
    iota妙用
    io
    http
    gosched
    go设置使用多少个cpu
    go协程的特点
    go条件变量同步机制
    Go奇技淫巧
    U5首次登录
    Maven安装中的问题
  • 原文地址:https://www.cnblogs.com/xinyf/p/5992004.html
Copyright © 2011-2022 走看看