#include <vector>
#include <string>
#include <iostream>
using namespace std;
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;
}
void SplitString(const std::string &s, std::vector<std::string> &v, const std::string &c)
{
std::string::size_type pos1, pos2;
pos2 = s.find(c);
pos1 = 0;
while (std::string::npos != pos2)
{
v.push_back(s.substr(pos1, pos2 - pos1));
pos1 = pos2 + c.size();
pos2 = s.find(c, pos1);
}
if (pos1 != s.length())
v.push_back(s.substr(pos1));
}
int main()
{
std::string a = "a, b, c, d,e,f,g,,s f fs fds ,, , hfx";
std::vector<std::string> result;
std::vector<std::string> result2 = split(a, ", ");
SplitString(a, result, ", ");
for (auto &&it : result)
{
std::cout << it;
}
std::cout << "\n";
for (auto &&it : result2)
{
std::cout << it << " ";
}
}
C++ spilt
最新推荐文章于 2023-07-16 21:37:19 发布