📜  c++ splitstring 示例 - C++ 代码示例

📅  最后修改于: 2022-03-11 14:44:51.391000             🧑  作者: Mango

代码示例3
#include 
#include 
#include 

using namespace std;

// for string delimiter
vector split (string s, string delimiter) {
    size_t pos_start = 0, pos_end, delim_len = delimiter.length();
    string token;
    vector res;

    while ((pos_end = s.find (delimiter, pos_start)) != string::npos) {
        token = s.substr (pos_start, pos_end - pos_start);
        pos_start = pos_end + delim_len;
        res.push_back (token);
    }

    res.push_back (s.substr (pos_start));
    return res;
}

int main() {
    string str = "adsf-+qwret-+nvfkbdsj-+orthdfjgh-+dfjrleih";
    string delimiter = "-+";
    vector v = split (str, delimiter);

    for (auto i : v) cout << i << endl;

    return 0;
}