Javatpoint Logo
Javatpoint Logo

Boost::split in c++ library

Strtok in C is comparable to this function. Tokens are created from the input sequence and are separated by separators. Through the predicate, separators are provided.


Application: A string is divided into substrings and separated from one another by separators using this technique.


Tokenize the Provided String Using the boost::split Function

Boost offers strong tools for adding mature, well-tested libraries to the C++ standard library. The boost::split function, which is a component of the Boost string algorithm library, is examined in this article. The latter has numerous techniques for manipulating strings, including clipping and replacing. The boost::split function divides a string sequence into tokens and separates them with a delimiter. The third parameter should be the delimiter, which the user should specify in a predicate function. If the given element is a delimiter, the offered function must return true.

To identify spaces in the provided text and separate them into tokens, we supply an isspace function object in the example that follows. To save tokenized substrings, boost::split also requires a destination sequence container. It should be noted that the function call overwrites the destination container's prior contents after the first parameter, which is the destination container itself.


Lorem; ipsum; ; dolor; sit; amet,; consectetur; adipiscing; elit.;

In the preceding code sample, the boost::split method stores empty strings whenever two or more delimiters are close to one another. Even yet, adjacent delimiters will be combined as seen in the following example if we specify the fourth optional parameter boost::token compress on. Keep in mind that these two code snippets won't work unless the Boost libraries are installed on the computer.


Lorem; ipsum; dolor; sit; amet,; consectetur; adipiscing; elit.;

Use stringstream With getline Function to Split the String With Delimiters

To split text with the specified character delimiter instead, use the getline function and the stringstream class. In this scenario, Boost headers are not necessary because we are simply using STL tools. Keep in mind that this version of the code is longer and requires additional steps to combine the neighboring delimiter letters.


Lorem; ipsum; ; dolor; sit; amet,; consectetur; adipiscing; elit.;


For one typical application case, split algorithms are an extension to the find iterator. All matches are stored in the given container by these algorithms, which employ a find iterator. The extracted substrings must be able to be stored in this container as references (such as iterator range) or copies (such as std::string).

There are two algorithms offered. In the input, find all() locates every instance of a string. split() divides the input into sections.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA