例子:

1
2
3
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

1
2
3
4
5
// 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::list>

1
2
3
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