ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ 分割字符串的一些方法

2021-07-24 23:34:09  阅读:449  来源: 互联网

标签:std 分割 string lastPos C++ split 字符串 view first


自定义 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有