c++ split實(shí)現(xiàn)
來源:程序員人生 發(fā)布時(shí)間:2014-11-11 08:39:06 閱讀次數(shù):3399次
template<typename _Fty> inline
void split(const std::string& s, const char* delims, _Fty op)
{
size_t start = 0;
size_t last = s.find_first_of(delims, start);
while (last != std::string::npos)
{
if (last > start)
op(s.substr(start, last - start));
last = s.find_first_of(delims, start = last + 1);
}
if (start < s.size())
op(s.substr(start));
}
inline
void split(const std::string& s, const std::string& delims, std::vector<std::string>& values)
{
split(s, delims, [&values](std::string&& item) { values.push_back(std::move(item)); } );
}
int main(int, char**)
{
std::vector<std::string> values;
split("hello#@ffdsdf#@ffgfdg#@ gdsfd @ af#", "#", values);
return 0;
}
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)