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