原文
实现与测试
#include <vector>
#include <string_view>
#include <iostream>
using namespace std;
std::vector<std::string_view> Split(std::string_view sv, char ch)
{
std::vector<std::string_view> points;
size_t point = 0;
for (size_t i = 0; i < sv.length(); i++)
{
if (sv[i] == ch)
{
points.emplace_back(std::string_view(&sv[point], i - point));
point = i + 1;
}
}
if (point >= sv.size())
{
points.emplace_back(std::string_view(&sv[point - 1], sv.size() - point));
}
else {
points.emplace_back(std::string_view(&sv[point], sv.size() - point));
}
return points;
}
int main()
{
string t = "-111,222-,333-444,-";
for (auto &&i : Split(t, ','))
{
for (auto &&j : Split(i, '-'))
{
std::cout << atoi(string(j).c_str()) << std::endl;
}
}
return 0;
}
运行结果