std::string实现split和trim方法

void split(const std::string& str, const std::string& strDelimiter,  std::vector<std::string>& result) {
    std::regex reg(strDelimiter);
    std::sregex_token_iterator pos(str.begin(), str.end(), reg, -1);
    decltype(pos) end;

    result.clear();
    for (; pos != end; ++pos) {
        result.emplace_back(pos->str());
    }
}

 

void trim(std::string& s) 
{
    s.erase(0, s.find_first_not_of(' '));
    s.erase(s.find_last_not_of(' ') + 1);
}

 

posted @ 2021-06-10 17:23  碎银三二两  阅读(771)  评论(0编辑  收藏  举报