c++ string split

#include <iterator>
#include <regex>
std::vector<std::string> s_split(const std::string& in, const std::string& delim) {
    std::regex re{ delim };
    // 调用 std::vector::vector (InputIterator first, InputIterator last,const allocator_type& alloc = allocator_type())
    // 构造函数,完成字符串分割
    return std::vector<std::string> {
        std::sregex_token_iterator(in.begin(), in.end(), re, -1),
            std::sregex_token_iterator()
    };
}


int main() {
    ;


    std::string s = "scott>=tiger>=mushroom";
    std::string delimiter = ">=";

    std::vector<std::string> res;
    res = s_split(s, delimiter);
    for (int i = 0; i < res.size(); i++)
        cout << res[i] << endl;
    
    
    system("pause");
}

 

posted @ 2019-01-03 20:04  乐乐章  阅读(3718)  评论(0编辑  收藏  举报