程序员人生 网站导航

c++ split实现

栏目:互联网时间:2014-11-11 08:39:06

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; }


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐