C++ 实现——split函数
2016-04-07 08:57
465 查看
//针对C++语言中没有现成的split函数,自己手动实现此函数,供以后直接使用
#include <iostream> #include <vector> #include <string> using namespace std; //处理函数 /** *s 原字符串 *seperator 分割符 *return 返回分好的串集合 */ vector<string> split(const string &s, const string &seperator){ vector<string> result; typedef string::size_type string_size; string_size i = 0; while (i != s.size()){ //找到字符串中首个不等于分隔符的字母; int flag = 0; while (i != s.size() && flag == 0){ flag = 1; for (string_size x = 0; x < seperator.size(); ++x) if (s[i] == seperator[x]){ ++i; flag = 0; break; } } //找到又一个分隔符,将两个分隔符之间的字符串取出; flag = 0; string_size j = i; while (j != s.size() && flag == 0){ for (string_size x = 0; x < seperator.size(); ++x) if (s[j] == seperator[x]){ flag = 1; break; } if (flag == 0) ++j; } if (i != j){ result.push_back(s.substr(i, j - i)); i = j; } } return result; } int main(){ string s = "hello boy, I am a student, come from shangdong"; vector<string> res = split(s, " "); for (auto a : res){ cout << a << endl; } return 0; }
相关文章推荐
- C++实验3项目1
- 第三周第二篇 本月有几天?
- C++作业3
- C++作业3
- 深度学习(七)caffe源码c++学习笔记
- 我的C++第三次作业
- c++实验3-个人所得税计算器
- C++作业3
- C++作业3
- C++课堂实验3
- 第三周第三题(2) 输出空心三角形
- c++实验2-分段函数求值
- C++实验3—个人所得税计算器
- c++第三次上机实验项目四
- C++实验3-本月有几天?
- C语言学习010:fopen读写文件
- 第2周 C语言及程序设计初步例程-37 循环嵌套
- CSP考试 2015年03月第4题 C语言实现
- CSP考试 2015年03月第5题 C语言实现
- C/C++不同文件夹下包含头文件的方法及#include的使用