#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include <unordered_map>
#include <functional>
#include <utility>
class string_utility {
public:
static void split_string(char *str, const char *delim, std::vector<std::string>&tokens) {
tokens.clear();
char *p = strtok(str, delim);
while (p != nullptr) {
tokens.emplace_back(p);
p = strtok(NULL, delim);
}
}
static void split_string(std::string &str, const char *delim, std::vector<std::string>&tokens) {
int pos = 0;
tokens.clear();
while (true) {
pos = str.find(delim, 0);
if (std::string::npos == pos) {
if (false == str.empty()) {
tokens.emplace_back(str);
}
break;
}
if (pos != 0) {
tokens.emplace_back(str.substr(0, pos));
}
str = str.substr(pos + 1, str.length());
}
}
};
int main() {
char str[] = "abc*ckddd*eeeef*lpoppp12 12* 111**aaaaa";
std::string str1 = "abc*ckddd*eeeef*lpoppp12 12* 111**aaaaa";
int loop = 10000000;
std::vector<std::string>tokens;
for (int i = 0;i < loop;i++) {
//string_utility::split_string(str, "*", tokens); // 0.935s
string_utility::split_string(str1, "*", tokens); // 0.530s
}
return 0;
}