boost 拆分字符串

7 篇文章 0 订阅
这篇博客介绍了如何利用Boost库中的split函数来拆分字符串。通过示例代码展示了如何将包含多个IP地址的字符串按分号分割,并将结果存储到std::vector中。split函数提供了强大的字符串分词能力,支持自定义分隔符并具有异常安全性。
摘要由CSDN通过智能技术生成

原创文章禁止转载boost 拆分字符串

例子:

std::vector<std::string> ipsVec;
ips="192.168.1.4;192.168.1.5;192.168.1.6;";
boost::split(ipsVec, ips, boost::is_any_of(";"));

Function template split

boost::algorithm::split — Split algorithm.

Synopsis

// In header: <boost/algorithm/string/split.hpp>
template<typename SequenceSequenceT, typename RangeT, typename PredicateT> 
  SequenceSequenceT & 
  split(SequenceSequenceT & Result, RangeT & Input, PredicateT Pred, 
        token_compress_mode_type eCompress = token_compress_off);

Description

Tokenize expression. This function is equivalent to C strtok. Input sequence is split into tokens, separated by separators. Separators are given by means of the predicate.

Each part is copied and added as a new element to the output container. Thus the result container must be able to hold copies of the matches (in a compatible structure like std::string) or a reference to it (e.g. using the iterator range class). Examples of such a container are or std::vectorstd::stringstd::list<boost::iterator_rangestd::string::iterator>

Note
Prior content of the result will be overwritten.
This function provides the strong exception-safety guarantee

Parameters:

Input

A container which will be searched.

Pred

A predicate to identify separators. This predicate is supposed to return true if a given element is a separator.

Result

A container that can hold copies of references to the substrings

eCompress

If eCompress argument is set to token_compress_on, adjacent separators are merged together. Otherwise, every two separators delimit a token.

Returns:

A reference the result
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值