从以其他字符分隔的字符串中取数字段

vector<std::string> extractNums(const std::string& s)
{
    vector<std::string> nums;
    bool extracting = false;
    auto begin = s.cbegin();
    for (auto it = s.cbegin(); it != s.cend(); ++it){
        if (extracting){
            if (!std::isdigit(*it)){
                nums.push_back (std::string(begin, it));
                extracting = false;
            }
        }else{
            if (std::isdigit(*it)){
                begin = it;
                extracting = true;
            }
        }
    }
    if (extracting){
        nums.push_back (std::string(begin, s.cend ()));
    }
    return nums;
}

 

posted @ 2015-12-31 14:49  wu_overflow  阅读(184)  评论(0)    收藏  举报