#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <iomanip>
using namespace std;
//索引实现
vector<string> split(const string &str)
{
vector<string> ret;
int start = 0, end = 0;
while (end < str.size())
{//非空字符一直走
while (end < str.size() && str[end] != ' ')
end++;
//截取
if (start < end)
{
ret.push_back(str.substr(start, end - start));
start = end;
}
//跳过连续空格
while (end < str.size() && str[end] == ' ')
{
end++;
start = end;
}
}
return ret;
}
//迭代器实现
bool is_space(char ch)
{
return ch == ' ';
}
bool not_space(char ch)
{
return ch != ' ';
}
vector<string> split_iter(const string & str)
{
vector<string> ret;
string::const_iterator start, end;
start = end = str.begin();
while (end != str.end())
{
//先查找空格
start = find_if(start, str.end(), not_space);
end = start;
//找到单词结尾
end = find_if(start, str.end(), is_space);
//截取
if (start != end)
{
ret.push_back(string(start, end));
start = end;
}
}
return ret;
}
c++ split的两种实现
最新推荐文章于 2022-06-08 00:49:56 发布