c++实现字符串切割

#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <vector>
using namespace std;
const int inf=999999;//0x7fffff

vector<string> split(string str,string del){
    vector<string> result;
    size_t pos=0;
    string token;
    while((pos=str.find(del))!=std::string::npos){
        token=str.substr(0,pos);
        result.push_back(token);
        str.erase(0,pos+del.length());
    }
    if(!str.empty()) result.push_back(str);
    return (result);
} 
int main()
{
    string str="hollo,world,nice to meet you";
    string del=",";
    vector<string> v=split(str,del);
    for(int i=0;i<v.size();i++) cout<<v[i]<<endl;
    return 0;
}

 

posted @ 2021-03-12 10:34  Maxwell·  阅读(181)  评论(0编辑  收藏  举报