1.简洁高效的方法(不过只能包含一个分隔符):

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. void SplitString(const string& s, vector<string>& v, const string& c)
  6. {
  7. string::size_type pos1, pos2;
  8. pos2 = s.find(c);
  9. pos1 = 0;
  10. while(string::npos != pos2)
  11. {
  12. v.push_back(s.substr(pos1, pos2-pos1));
  13. pos1 = pos2 + c.size();
  14. pos2 = s.find(c, pos1);
  15. }
  16. if(pos1 != s.length())
  17. v.push_back(s.substr(pos1));
  18. }
  19. int main(){
  20. string s = "a,b,c,d,e,f";
  21. vector<string> v;
  22. SplitString(s, v,","); //可按多个字符来分隔;
  23. for(vector<string>::size_type i = 0; i != v.size(); ++i)
  24. cout << v[i] << " ";
  25. cout << endl;
  26. //输出: a b c d e f
  27. }

当处理有空格的字符串时,还是很有用的!!
使用void SplitString(const string& s, vector& v, const string& c),和将v作为返回值都是可以的!

2.可包含多个分隔符的实现方式

  1. #include <vector>
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5. vector<string> split(const string &s, const string &seperator){
  6. vector<string> result;
  7. typedef string::size_type string_size;
  8. string_size i = 0;
  9. while(i != s.size()){
  10. //找到字符串中首个不等于分隔符的字母;
  11. int flag = 0;
  12. while(i != s.size() && flag == 0){
  13. flag = 1;
  14. for(string_size x = 0; x < seperator.size(); ++x)
  15. if(s[i] == seperator[x]){
  16. ++i;
  17. flag = 0;
  18. break;
  19. }
  20. }
  21. //找到又一个分隔符,将两个分隔符之间的字符串取出;
  22. flag = 0;
  23. string_size j = i;
  24. while(j != s.size() && flag == 0){
  25. for(string_size x = 0; x < seperator.size(); ++x)
  26. if(s[j] == seperator[x]){
  27. flag = 1;
  28. break;
  29. }
  30. if(flag == 0)
  31. ++j;
  32. }
  33. if(i != j){
  34. result.push_back(s.substr(i, j-i));
  35. i = j;
  36. }
  37. }
  38. return result;
  39. }
  40. int main(){
  41. // string s = "a,b*c*d,e";
  42. string s;
  43. getline(cin,s);
  44. vector<string> v = split(s, ",*"); //可按多个字符来分隔;
  45. for(vector<string>::size_type i = 0; i != v.size(); ++i)
  46. cout << v[i] << " ";
  47. cout << endl;
  48. //输出: a b c d e
  49. }

3. 用C语言中的strtok 函数来进行分割

原型: char _strtok(char _str, const char *delim);strtok函数包含在头文件中,对于字符数组可以采用这种方法处理。

  1. #include <string.h>
  2. #include <stdio.h>
  3. int main(){
  4. char s[] = "a,b*c,d";
  5. const char *sep = ",*"; //可按多个字符来分割
  6. char *p;
  7. p = strtok(s, sep);
  8. while(p){
  9. printf("%s ", p);
  10. p = strtok(NULL, sep);
  11. }
  12. printf("\n");
  13. return 0;
  14. }
  15. //输出: a b c d

From https://www.cnblogs.com/carsonzhu/p/5859552.html

4 最优雅的实现

  1. void split(const std::string& s, std::vector<std::string>& tokens, const std::string& delimiters = " ")
  2. {
  3. std::string::size_type lastPos = s.find_first_not_of(delimiters, 0);
  4. std::string::size_type pos = s.find_first_of(delimiters, lastPos);
  5. while (std::string::npos != pos || std::string::npos != lastPos)
  6. {
  7. tokens.push_back(s.substr(lastPos, pos - lastPos));
  8. lastPos = s.find_first_not_of(delimiters, pos);
  9. pos = s.find_first_of(delimiters, lastPos);
  10. }
  11. }