How can I convert a std::string to int?

后端 未结 19 1068
梦毁少年i
梦毁少年i 2020-11-22 02:10

Just have a quick question. I\'ve looked around the internet quite a bit and I\'ve found a few solutions but none of them have worked yet. Looking at converting a string to

相关标签:
19条回答
  • 2020-11-22 02:51

    My Code:

    #include <iostream>
    using namespace std;
    
    int main()
    {
        string s="32";  //String
        int n=stoi(s);  //Convert to int
        cout << n + 1 << endl;
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-22 02:53

    To be more exhaustive (and as it has been requested in comments), I add the solution given by C++17 using std::from_chars.

    std::string str = "10";
    int number;
    std::from_chars(str.data(), str.data()+str.size(), result);
    

    If you want to check whether the conversion was successful:

    std::string str = "10";
    int number;
    auto [ptr, ec] = std::from_chars(str.data(), str.data()+str.size(), result);
    assert(ec == std::errc{});
    // ptr points to chars after read number
    

    Moreover, to compare the performance of all these solutions, see the following quick-bench link: https://quick-bench.com/q/GBzK53Gc-YSWpEA9XskSZLU963Y

    (std::from_chars is the fastest and std::istringstream is the slowest)

    0 讨论(0)
  • 2020-11-22 02:57

    To convert from string representation to integer value, we can use std::stringstream.

    if the value converted is out of range for integer data type, it returns INT_MIN or INT_MAX.

    Also if the string value can’t be represented as an valid int data type, then 0 is returned.

    #include 
    #include 
    #include 
    
    int main() {
    
        std::string x = "50";
        int y;
        std::istringstream(x) >> y;
        std::cout << y << '\n';
        return 0;
    }
    

    Output: 50

    As per the above output, we can see it converted from string numbers to integer number.

    Source and more at string to int c++

    0 讨论(0)
  • 2020-11-22 03:03

    Admittedly, my solution wouldn't work for negative integers, but it will extract all positive integers from input text containing integers. It makes use of numeric_only locale:

    int main() {
            int num;
            std::cin.imbue(std::locale(std::locale(), new numeric_only()));
            while ( std::cin >> num)
                 std::cout << num << std::endl;
            return 0;
    }
    

    Input text:

     the format (-5) or (25) etc... some text.. and then.. 7987...78hjh.hhjg9878
    

    Output integers:

     5
    25
    7987
    78
    9878
    

    The class numeric_only is defined as:

    struct numeric_only: std::ctype<char> 
    {
        numeric_only(): std::ctype<char>(get_table()) {}
    
        static std::ctype_base::mask const* get_table()
        {
            static std::vector<std::ctype_base::mask> 
                rc(std::ctype<char>::table_size,std::ctype_base::space);
    
            std::fill(&rc['0'], &rc[':'], std::ctype_base::digit);
            return &rc[0];
        }
    };
    

    Complete online demo : http://ideone.com/dRWSj

    0 讨论(0)
  • 2020-11-22 03:04

    use the atoi function to convert the string to an integer:

    string a = "25";
    
    int b = atoi(a.c_str());
    

    http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

    0 讨论(0)
  • 2020-11-22 03:05

    It's probably a bit of overkill, but boost::lexical_cast<int>( theString ) should to the job quite well.

    0 讨论(0)
提交回复
热议问题