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");
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM