<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          實(shí)現(xiàn)字符串split的6種方法

          共 5859字,需瀏覽 12分鐘

           ·

          2021-05-28 17:59

          眾所周知C++標(biāo)準(zhǔn)庫沒有提供std::string的split功能,究竟為什么沒有提供split方法,我查了很多資料,網(wǎng)上也有很多說法,但是依舊沒有找到官方答案。


          既然沒有,那我們不如......

          按自己的方法實(shí)現(xiàn)一個(gè)好了。

          如果項(xiàng)目庫里集成了boost的話,可以直接使用boost的split功能,我這里也列出了6種實(shí)現(xiàn)split的方法,分享一下,希望大家能拓寬下思路。

          方法1:stringstream和getline配合使用
          std::vector<std::string> stringSplit(const std::string& str, char delim) {    std::stringstream ss(str);    std::string item;    std::vector<std::string> elems;    while (std::getline(ss, item, delim)) {        if (!item.empty()) {            elems.push_back(item);        }    }    return elems;}

          方法2:使用std::string::find
          std::vector<std::string> stringSplit(const std::string& str, char delim) {    std::size_t previous = 0;    std::size_t current = str.find(delim);    std::vector<std::string> elems;    while (current != std::string::npos) {        if (current > previous) {            elems.push_back(str.substr(previous, current - previous));        }        previous = current + 1;        current = str.find(delim, previous);    }    if (previous != str.size()) {        elems.push_back(str.substr(previous));    }    return elems;}

          方法3:使用std::string::find_first_of
          std::vector<std::string> stringSplit(const std::string& str, char delim) {    std::size_t previous = 0;    std::size_t current = str.find_first_of(delim);    std::vector<std::string> elems;    while (current != std::string::npos) {        if (current > previous) {            elems.push_back(str.substr(previous, current - previous));        }        previous = current + 1;        current = str.find_first_of(delim, previous);    }    if (previous != str.size()) {        elems.push_back(str.substr(previous));    }    return elems;}

          方法4:使用C語言的strtok方法
          std::vector<std::string> stringSplit(const std::string& strIn, char delim) {    char* str = const_cast<char*>(strIn.c_str());    std::string s;    s.append(1, delim);    std::vector<std::string> elems;    char* splitted = strtok(str, s.c_str());    while (splitted != NULL) {        elems.push_back(std::string(splitted));        splitted = strtok(NULL, s.c_str());    }    return elems;}

          方法5:std::string::find_first_of和std::string::find_first_not_of配合使用
          std::vector<std::string> stringSplit(const std::string& str, char delim) {    std::vector<std::string> elems;    auto lastPos = str.find_first_not_of(delim, 0);    auto pos = str.find_first_of(delim, lastPos);    while (pos != std::string::npos || lastPos != std::string::npos) {        elems.push_back(str.substr(lastPos, pos - lastPos));        lastPos = str.find_first_not_of(delim, pos);        pos = str.find_first_of(delim, lastPos);    }    return elems;}

          方法6:使用正則表達(dá)式
          std::vector<std::string> stringSplit(const std::string& str, char delim) {    std::string s;    s.append(1, delim);    std::regex reg(s);    std::vector<std::string> elems(std::sregex_token_iterator(str.begin(), str.end(), reg, -1),                                   std::sregex_token_iterator());    return elems;}

          參考資料
          https://www.zhihu.com/question/36642771
          http://www.martinbroadhurst.com/how-to-split-a-string-in-c.html


          瀏覽 45
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品一级毛片无码视频 | 免费看美女黄片操逼视频 | 玖玖精品一区二区 | 欧洲性爱在线 | 国产精品青青 |