#include <string>
#include <sstream>
#include <vector>
void split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
elems.push_back(item);
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
item.empty()
测试代码 -
#include <xxx>
using namespace std;
string str = "0x0 0";
vector<string> result = split (str, ' ');
结果应该是 -
["0x0", "0"]
但它没有拆分字符串,结果是 -
["0x0 0"]
但是,它在 -
时工作正常str = "a b c d ";
使用std::getline()
时有什么遗漏吗?