C++一行代码都不浪费的字符串分割
时间:2022-05-27 21:58:20
收藏:0
阅读:17
网上的都不满意 直接手搓 复制可用
网上的都不满意 直接手搓 复制可用
网上的都不满意 直接手搓 复制可用
#include <cstdio> #include <string> #include <vector> void KsaDiv(std::vector<std::string>& vstr, std::string &orgs, char divs) { size_t _index = 0; vstr.clear(); const char* _bef = orgs.data(); while (_index < orgs.size()) { if (orgs[_index] == divs) { orgs[_index++] = 0; vstr.push_back(_bef); _bef = &orgs[_index]; continue; } _index++; } if (_bef[0] && vstr.size()) vstr.push_back(_bef); orgs.resize(0); } void KsaDivEx(std::vector<std::string>& vstr, std::string& orgs, std::string divs) { size_t _index = 0; vstr.clear(); const char* _bef = orgs.data(); while ((_index = orgs.find(divs, _index)) < orgs.size()) { orgs[_index] = 0; _index += divs.size(); vstr.push_back(_bef); _bef = &orgs[_index]; } if (_bef[0] && vstr.size()) vstr.push_back(_bef); orgs.resize(0); } int main(int arg, int* argv[]) { std::vector<std::string> vstr; std::string org = "1,9,8,55,6"; KsaDiv(vstr, org, ‘,‘); for (size_t i = 0; i < vstr.size(); i++) { printf("[%llu]%s\n",i, vstr[i].data()); } org = "1,,9,,8,,55,,6"; KsaDivEx(vstr, org, ",,"); for (size_t i = 0; i < vstr.size(); i++) { printf("Ex[%llu]%s\n",i, vstr[i].data()); } system("pause"); } // 20210925 ksanl888@qq.com OpenSourse
原文:https://www.cnblogs.com/ksanl/p/15334487.html
评论(0)