#include <iostream>
#include <vector>
void split(const std::string& src, const std::string& delim, std::vector< std::string >& ret)
{
size_t last = 0;
size_t index = src.find_first_of(delim, last);
while (index != std::string::npos)
{
ret.push_back(src.substr(last, index - last));
last = index + 1;
index = src.find_first_of(delim, last);
}
if (index - last > 0)
{
ret.push_back(src.substr(last, index - last));
}
}
int main(int argc, char** argv)
{
std::string str = "123,abcd,ooo";
std::vector<std::string> vecInfo;
split(str, ",", vecInfo);
std::vector<std::string>::iterator iter = vecInfo.begin();
for (; iter != vecInfo.end(); ++iter)
{
std::cout << iter->c_str() << std::endl;
}
system("pause");
}
结果:
123
abcd
ooo