C++ 分割字符串的一些方法
作者:互联网
自定义 split 函数
1 void split(const string& s, vector<string>& tokens, const string& delimiters = " ") 2 { 3 string::size_type lastPos = s.find_first_not_of(delimiters, 0); 4 string::size_type pos = s.find_first_of(delimiters, lastPos); 5 while (string::npos != pos || string::npos != lastPos) { 6 tokens.emplace_back(s.substr(lastPos, pos - lastPos)); 7 lastPos = s.find_first_not_of(delimiters, pos); 8 pos = s.find_first_of(delimiters, lastPos); 9 } 10 }
使用正则表达式 (C++ 11)
1 std::string text = "Quick brown fox."; 2 std::regex ws_re("\\s+"); // whitespace 3 std::vector<std::string> v(std::sregex_token_iterator(text.begin(), text.end(), ws_re, -1), 4 std::sregex_token_iterator()); 5 for(auto&& s: v) 6 std::cout<<s<<"\n";
使用 istringstream 和 getline 实现分割
1 void split(vector<string> &strings, string data) { 2 istringstream f(data); 3 vector<string> strings; 4 string s; 5 char sep = ','; 6 while (getline(f, s, sep)) { 7 strings.push_back(s); 8 } 9 }
使用 strtok 函数分割 C 风格字符串 (char *)
1 #include <string.h> 2 #include <iostream> 3 #include <string> 4 using namespace std; 5 int main() 6 { 7 string str = "one two three four five"; 8 char *token = strtok(str.data(), " "); // non-const data() needs c++17 9 while (token != NULL) { 10 std::cout << token << '\n'; 11 token = strtok(NULL, " "); 12 } 13 }
strtok的第一个参数类型是 char * 而不是 const char *,它会改变输入的字符串。
使用 string_view 改进第一个 split 函数 (C++ 17)
1 std::vector<std::string_view> 2 splitSV(std::string_view strv, std::string_view delims = " ") 3 { 4 std::vector<std::string_view> output; 5 size_t first = 0; 6 7 while (first < strv.size()) 8 { 9 const auto second = strv.find_first_of(delims, first); 10 11 if (first != second) 12 output.emplace_back(strv.substr(first, second-first)); 13 14 if (second == std::string_view::npos) 15 break; 16 17 first = second + 1; 18 } 19 20 return output; 21 }
string_view 相比 string 减少了拷贝,提升了性能
参考:Bartek's coding blog: Speeding Up string_view String Split Implementation (bfilipek.com)
C++ 20 的标准库中提供了 ranges,有专门的 split view,使用 str | split(' ') 切分字符串
1 string str("hello world test split"); 2 auto sv = str 3 | ranges::views::split(' ') 4 | ranges::views::transform([](auto&& i){ 5 return i | ranges::to<string>(); }) 6 | ranges::to<vector>(); 7 8 for(auto&& s: sv) { 9 cout<<s<<"\n"; 10 }
标签:std,分割,string,lastPos,C++,split,字符串,view,first 来源: https://www.cnblogs.com/rider101/p/15056693.html