zoukankan      html  css  js  c++  java
  • c++ split模板实现

    模板实现,重载+6:

    template<typename _Elem, typename _Fty> inline
    void split(const _Elem* s, const _Elem delim, _Fty op)
    {
        const _Elem* _Start = s; // the start of every string
        const _Elem* _Ptr = s;   // source string iterator
        while( *_Ptr != '' )
        {
            if(delim == *_Ptr/* && _Ptr != _Start*/) 
            {
                if (_Ptr != _Start)
                    op(std::basic_string<_Elem>(_Start, _Ptr));
                _Start = _Ptr + 1;
            }
            ++_Ptr;
        }
        if(_Start != _Ptr) {
            op(std::basic_string<_Elem>(_Start, _Ptr));
        }
    }
    
    template<typename _Elem, typename _Fty> inline
    void split(const _Elem* s, const _Elem* delims, _Fty op)
    {
        const _Elem* _Start = s; // the start of every string
        const _Elem* _Ptr = s;   // source string iterator
        size_t      _Lend = strlen(delims);
        while ((_Ptr = strstr(_Ptr, delims)) != nullptr)
        {
            if (_Ptr != _Start)
            {
                op(std::basic_string<_Elem>(_Start, _Ptr));
            }
            _Start = _Ptr + _Lend;
            _Ptr += _Lend;
        }
        if (*_Start) {
            op(std::basic_string<_Elem>(_Start));
        }
    }
    
    /* any of delims */
    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));
        }
    }
    
    template<typename _Elem> inline
    std::vector<std::string> split(const _Elem* s, const _Elem delim)
    {
        std::vector<std::basic_string<_Elem> > output;
        nsc::split(s, delim, [&output](std::basic_string<_Elem>&& value)->void{
            output.push_back(std::move(value));
        });
        return std::move(output);
    }
    
    template<typename _Elem> inline
    std::vector<std::string> split(const _Elem* s, const _Elem* delims)
    {
        std::vector<std::basic_string<_Elem> > output;
        nsc::split(s, delims, [&output](std::basic_string<_Elem>&& value)->void{
            output.push_back(std::move(value));
        });
        return std::move(output);
    }
    
    inline
    std::vector<std::string> split(const std::string& s, const char* delim )
    {
        std::vector< std::string > output;
        nsconv::split(s, delim, [&output](std::string&& value)->void {
            output.push_back(std::move(value));
        });
        return std::move(output);
    }

    測试代码:

    
    int main(int, char**)
    {
    
        std::vector<std::string> values;
    
        split("#hello#@ffdsdf#@ffgfdg#@ gdsfd @ af#", "#", values);
    
        return 0;
    }
    

    
  • 相关阅读:
    数组——遍历
    数组常用方法——汇总
    箭头函数
    overflow:auto产生的滚动条在安卓系统下能平滑滚动,而在ios下滚动不平滑
    浅谈过载保护
    tornado中使用torndb,连接数过高的问题
    如何开启并查看博客园MetaWeblog接口的xml-RPC协议访问地址
    aardio陷阱(转)
    aardio获取公网IP地址代码
    sql常用函数学习笔记整理
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5202733.html
Copyright © 2011-2022 走看看