Convert std::string to integer

后端 未结 4 670
没有蜡笔的小新
没有蜡笔的小新 2021-02-05 08:27

I\'m trying to convert a std::string stored in a std::vector to an integer and pass it to a function as a parameter.

This is a simplified vers

相关标签:
4条回答
  • 2021-02-05 08:46
    #include <boost/lexical_cast.hpp>
    
    functiontest(boost::lexical_cast<int>(record[i]));
    
    0 讨论(0)
  • 2021-02-05 08:49

    Use stringstream from standard library. It's cleaner and it's rather C++ than C.

    int i3;
    std::stringstream(record[i]) >> i3; 
    
    0 讨论(0)
  • 2021-02-05 08:51
    record[i].c_str
    

    is not the same as

    record[i].c_str()
    

    You can actually get this from the error message: the function expects a const char*, but you're providing an argument of type const char* (std::basic_string<char, std::char_traits<char>, std::allocator<char> >::)()const which is a pointer to a member function of the class std::basic_string<char, std::char_traits<char>, std::allocator<char> > that returns a const char* and takes no arguments.

    0 讨论(0)
  • 2021-02-05 08:52

    With C++11:

    int value = std::stoi(record[i]);
    
    0 讨论(0)
提交回复
热议问题