How do I convert vector of strings into vector of integers in C++?

前端 未结 7 2056
甜味超标
甜味超标 2021-01-23 01:10

I have a vector of strings. Need help figuring out how to convert it into vector of integers in order to be able to work with it arithmetically. Thanks!

#include         


        
7条回答
  •  情话喂你
    2021-01-23 01:29

    #include 
    #include 
    
    #include 
    #include 
    #include 
    
    using namespace std;
    
    int stringToInteger(const std::string& s)
    {
        return boost::lexical_cast(s);
    }
    
    int main(int /*argc*/, char* /*argv*/[])
    {
        vector vectorOfStrings;
    
        // ..
    
        vector vectorOfIntegers;
        std::transform(vectorOfStrings.begin(), vectorOfStrings.end(), std::back_inserter(vectorOfIntegers), stringToInteger);
    
        // ..
    }
    

    You can replace the implementation of stringToInteger(..) with your preferred conversion function.

提交回复
热议问题