码迷,mamicode.com
首页 > 编程语言 > 详细

c++ split 模板实现

时间:2014-10-29 21:37:45      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:blog   os   ar   sp   on   2014   art   log   bs   

template<typename _Fty> inline
void split(const std::string& s, const std::string& delims, _Fty op)
{
    size_t start = 0;
    size_t last = s.find(delims, start);
    while (last != std::string::npos)
    {
        op(std::move(s.substr(start, last - start)));

        last = s.find(delims, (start = last + delims.size()));
    }
    if (start < s.size())
    {
        op(std::move(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;
}

c++ split 模板实现

标签:blog   os   ar   sp   on   2014   art   log   bs   

原文地址:http://blog.csdn.net/xseekerj/article/details/40592829

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!