#include <iostream>
#include <vector>
std::vector<std::string> split_str(std::string& str , const std::string& separator)
{
size_t pos = 0;
std::vector<std::string> result_str;
while((pos = str.find(separator)) != std::string::npos) //只要找得到separator
{
std::string token = str.substr(0,pos);
result_str.push_back(token);
str.erase(0, pos + separator.length());
}
if(str.size() != 0)
result_str.push_back(str);
return result_str;
}
int main() {
std::string s = "asdf#@#sdghdf#@#hsdfags#@#ampk#@#123";
std::string separator = "#@#";
std::vector<std::string> result = split_str(s,separator);
for(int i = 0; i < result.size(); ++i)
std::cout << result[i] << std::endl;
return 0;
}
标签:std,分割,string,pos,C++,separator,result,str,字符串
From: https://www.cnblogs.com/horizonhz/p/17014964.html