sh-5.1$ cat main.cpp
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
std::vector<std::string> split(const std::string &str, char sep) {
std::vector<std::string> result;
std::string::const_iterator b = str.begin();
while(1) {
std::string::const_iterator e = std::find(b, str.end(), sep);
result.push_back(std::string(b, e));
if(e == str.end()) break;
b = e + 1;
}
return result;
}
std::ostream &operator<<(std::ostream &os, const std::vector<std::string> &v) {
bool flag = false;
for(const auto &i: v) {
if(flag) os << std::endl;
flag = true;
os << i;
}
return os;
}
int main() {
{
std::vector<std::string> v = split("12345", '.');
std::cout << v << std::endl;
}
std::cout << "**************************" << std::endl;
std::cout << split(".12345", '.') << std::endl;
std::cout << "**************************" << std::endl;
std::cout << split("12.345", '.') << std::endl;
std::cout << "**************************" << std::endl;
std::cout << split("12345.", '.') << std::endl;
std::cout << "**************************" << std::endl;
std::cout << split(".1.23.45.", '.') << std::endl;
return 0;
}
sh-5.1$ ./main
12345
**************************
12345
**************************
12
345
**************************
12345
**************************
1
23
45
sh-5.1$
|