日本黄色一级经典视频|伊人久久精品视频|亚洲黄色色周成人视频九九九|av免费网址黄色小短片|黄色Av无码亚洲成年人|亚洲1区2区3区无码|真人黄片免费观看|无码一级小说欧美日免费三级|日韩中文字幕91在线看|精品久久久无码中文字幕边打电话

當(dāng)前位置:首頁 > 單片機(jī) > 程序喵大人


如果項(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;}
本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請聯(lián)系該專欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請及時(shí)聯(lián)系本站刪除。
關(guān)閉