文中的字符串split函数功能是 从字符串中按照特定的分隔符进行分割,分割的结果保存到std::vector中。

1. strtok实现

std::vector<std::string> split(const char *s, const char *delim)
{
    std::vector<std::string> result;
    if (s && strlen(s))
    {
        int len = strlen(s);
        char *src = new char[len + 1];
        strcpy(src, s);
        src[len] = '\0';
        char *tokenptr = strtok(src, delim);
        while (tokenptr != NULL)
        {
            std::string tk = tokenptr;
            result.emplace_back(tk);
            tokenptr = strtok(NULL, delim);
        }
        delete[] src;
    }
    return result;
}
2.  优化版的字符串查找方式实现
void supersplit(const std::string& s, std::vector<std::string>& v, const std::string& c)
{
    std::string::size_type pos1, pos2;
    size_t len = s.length();
    pos2 = s.find(c);
    pos1 = 0;
    while(std::string::npos != pos2)
    {
        v.emplace_back(s.substr(pos1, pos2-pos1));
 
        pos1 = pos2 + c.size();
        pos2 = s.find(c, pos1);
    }
    if(pos1 != len)
        v.emplace_back(s.substr(pos1));
}
 
3. 正则表达式方式
std::vector<std::string> regexsplit(const std::string& input)

std::regex re(",");

    std::sregex_token_iterator p(input.begin(), input.end(), re, -1);
    std::sregex_token_iterator end;
    std::vector<std::string> vec;
    while (p != end)
        vec.emplace_back(*p++);
 
    return vec;
}
 
4. 使用boost split 实现
std::vector<std::string> boostsplit(const std::string& input)
{
    std::vector <std::string> fields;
    boost::split( fields, input, boost::is_any_of( "," ) );
    return fields;
}

5. sds  sdssplitlen 函数实现

void sdssplit(const std::string& input, std::vector<std::string>& v)

{

int len = 0;
sds* array = sdssplitlen(input.c_str(),input.length(),",",1,&len);
for(int i=0;i<len;i++)
  v.emplace_back(array[i]);

}

以上函数用Celero库测试的结果如下:

几种c++字符串split 函数实现的比较的更多相关文章

  1. c++字符串split 函数实现

    - 经常遇到字符串分割问题,但是相对于c++而言实现比较麻烦,直接遍历一遍也很冗余 - 另外也适用于,在字符串中找到某个字符的所有位置 //函数功能:将输入字符串s,以字符串c(;)进行拆分,拆分结果 ...

  2. 字符串split函数

    https://www.cnblogs.com/hjhsysu/p/5700347.html 函数:split() Python中有split()和os.path.split()两个函数,具体作用如下 ...

  3. Java字符串split函数的注意事项

    Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, Str ...

  4. python字符串、字符串处理函数及字符串相关操作

    python字符串.字符串处理函数及字符串相关操作 字符串介绍 python字符串表示 Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹: >>> 'spam e ...

  5. 飘逸的python - 增强的格式化字符串format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  6. Python 的格式化字符串format函数

    阅读mattkang在csdn中的博客<飘逸的python - 增强的格式化字符串format函数>所做笔记 自从python2.6开始,新增了一种格式化字符串的函数str.format( ...

  7. 强大的字符串格式化函数 - format

    自python2.6开始,新增了一种格式化字符串的函数str.format(),它通过{}和:来代替% 位置方法格式化 >>>'{}-{}'.format('simon','ting ...

  8. 格式化字符串format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  9. (转)飘逸的python - 增强的格式化字符串format函数

    原文:https://blog.csdn.net/handsomekang/article/details/9183303 Python字符串格式化--format()方法-----https://b ...

随机推荐

  1. Zuul上实现限流(spring-cloud-zuul-ratelimit)

    简述 Spring Cloud Zuul RateLimit项目Github地址: https://github.com/marcosbarbero/spring-cloud-zuul-ratelim ...

  2. 适用于app.config与web.config的ConfigUtil读写工具类

    之前文章:<两种读写配置文件的方案(app.config与web.config通用)>,现在重新整理一个更完善的版本,增加批量读写以及指定配置文件路径,代码如下: using System ...

  3. Understanding ROS Services and Parameters

    service是nodes之间通信的一种方式,允许nodes send a request and recieve a response. rosservice rosparam roservice ...

  4. 结合JDK源码看设计模式——享元模式

    前言 在说享元模式之前,你一定见到过这样的面试题 public class Test { public static void main(String[] args) { Integer a=Inte ...

  5. CANVAS画布与SVG的区别

    CANVAS是html5提供的新元素<canvas>,而svg存在的历史要比canvas久远,svg并不是html5专有的标签,最初svg是用xml技术(超文本扩展语言,可以自定义标签或属 ...

  6. SAP MM 实施项目里Open PO 迁移思路探讨

    SAP MM 实施项目里Open PO 迁移思路探讨 .序言.   SAP项目上线前夕,除了静态主数据需要导入以外,可能还有一些动态数据,比如open的采购订单,open的销售订单等单据也要迁移到SA ...

  7. 华为防火墙USG6000V使用总结

    问题1.ge 1/0/0 的ip地址 20.0.0.2 ,从直连的对端20.0.0.1 无法ping. 但是从防火墙ping对端却是可以ping通? 原因: 华为新一代的防火墙,默认情况下,只有0口是 ...

  8. arcgis for js开发之路径分析

    arcgis for js开发之路径分析 //方法封装 function routeplan(x1, x2, y1, y2, barrierPathArray, isDraw, callback) { ...

  9. iOS----------禁止输入汉字

    说明: ^.*[\u4e00-\u9fa5].*$ 是否包含中文^[\u4E00-\u9FA5]+$ 是否全中文 - (BOOL)textField:(UITextField *)textField ...

  10. 在非activity类调用startActivityForResult

    对于这个问题,今天折腾了一下午,不是说我不懂得怎么调用,而是我用了看似正确的调用方式,而其实这是一个坑. 我用了下面这种方式: ((Activity) mContext).startActivityF ...