boost::split()的使用方式

36 篇文章 2 订阅
21 篇文章 0 订阅

引用的头文件 <boost/algorithm/string.hpp>

boost::split()函数用于切割string字符串,将切割之后的字符串放到一个std::vector<std::string> 之中;

有4个参数:

以boost::split(type, select_list, boost::is_any_of(","), boost::token_compress_on);

(1)、type类型是std::vector<std::string>,用于存放切割之后的字符串

(2)、select_list:传入的字符串,可以为空。

(3)、boost::is_any_of(","):设定切割符为,(逗号)

(4)、 boost::token_compress_on:将连续多个分隔符当一个,默认没有打开,当用的时候一般是要打开的。

结果:

根据结果可以看出:

split()切割的string为空时,是可以切割出来一个空字符的。

另一种情况,如果string中有连续的,,时的处理如下:

  std::string s1("hash,project,,string");
  std::vector<std::string> split_string;

  boost::split(split_string, s1, boost::is_any_of(","), boost::token_compress_on);
    std::string err("size=");
    err += std::to_string(split_string.size());
    LogErr(WARNING_LEVEL, ER_LOG_PRINTF_MSG, err.c_str());

    for (std::string s : split_string) {
      std::string ans(s);
      LogErr(WARNING_LEVEL, ER_LOG_PRINTF_MSG, ans.c_str());
    }

结果

size=3
hash
project
string

总结:boost里面考虑的异常情况还是比较多的,还是比较优秀的开发工具

  • 7
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值