C++ string split函数实现

来源:互联网 发布:天猫美工工资 编辑:程序博客网 时间:2024/05/16 15:53

使用了string的find函数和substr函数

#include <iostream>#include <string>#include <vector>using namespace std;vector<string> split(string str, string pattern) {    vector<string> result;    str += pattern;    for (int i = 0; i < str.size(); i++) {        int pos = (int)str.find(pattern, i);        if (pos != string::npos) {            result.push_back(str.substr(i, pos - i));            i = pos + (int)pattern.size() - 1;        }    }    return result;}int main() {    string s = "I have a dream!";    vector<string> res = split(s, " ");    return 0;}
0 0
原创粉丝点击