源码版
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const vector<string> explode(const string &s,const char &c){
string buff;
vector<string> v;
char tmp;
for (int i=0;i<s.size();i++)
{
tmp = s[i];
if (tmp!=c){
buff+=tmp;
}else {
if(tmp==c && buff !=""){
v.push_back(buff);
buff = "";
}
}//endif
}
if (buff !="")
{
v.push_back(buff);
}
return v;
}
int main(){
string str("the quick brown fox jumps over the lazy dog");
vector<string> v = explode(str, ' ');
for(int i = 0;i<v.size();i++)
{
cout << v[i] << endl;
}
return 0;
}
strtok函数
int main(){
string str="the quick brown fox jumps over the lazy dog";
char const delimeters[] = " ";
char *ins = const_cast<char *>(str.data());
char *tok = strtok(ins,delimeters);
while(0!=tok){
puts(tok);
tok = strtok(NULL,delimeters);
}
return 0;
}