How to parse comma delimited integers from a string in c++

后端 未结 4 599
轻奢々
轻奢々 2021-01-16 00:48

I have a function recieving a string, which is a pair of comma delimited integers in a fashion such as \"12,4\". How can I parse the integers out of this string?

4条回答
  •  感动是毒
    2021-01-16 01:31

    Depends whether or not you can depend on the incoming data being valid. If you can, I'd do:

    #include 
    #include 
    #include 
    
    std::pair split(std::string const& str)
    {
        int const a = std::atoi(str.c_str());
        int const b = std::atoi(str.c_str() + str.find(',') + 1);
        return std::make_pair(a, b);
    }
    

提交回复
热议问题