C++ boost::algorithm::string::split()














































C++ boost::algorithm::string::split()



split():

     This function is included in the "boost/algorithm/string" library. This library contains some brilliant methods which help in accomplishing string manipulations that are lacking in STL library. 

This function is used to tokenize the given input string. It splits the given string with respect to the given delimiter character (ignoring the delimiter) and stores the resultant tokens in a container provided by the user. Each token of the string is copied as a new element in the container. Popularly used output containers are std::vector<std::string> or std::list<boost::iterator_range<std::string::iterator>>

header file:

boost/algorithm/string.hpp

syntax:

     split(output_container,input_string,predicate,eCompress);

parameters:

    input_string : an input string

output_container : the container to hold the copies or references to the substrings

predicate : A function that determines the delimiter.

eCompress : if token_compress_on then it compresses the adjacent tokens.

The user may choose to define his own predicate function which returns true for delimiter or may choose to use any predefined library function to determine the delimiter.

SAMPLE CODE :

#include<iostream> #include<boost/algorithm/string.hpp> #include<vector> using namespace std; using namespace boost::algorithm; bool func1(char c) { return c == ' '; } bool func2(char c) { return c == 'e'; } int main() { //declaring the input strings string str1 = "I am Boost Library"; string str2 = "I am very efficient"; //declaring the containers used for storing the resulting tokens vector<string> v1; vector<string> v2; split(v1,str1,func1,token_compress_on);//splitting by using whitespace as delimiter split(v2,str2,func2,token_compress_on);//splitting by using 'e' as delimiter //displaying the contents of v1 cout<<"String 1 :- "<<endl; for(int i=0;i<v1.size();i++) cout<<v1[i]<<endl; cout<<endl<<"String 2 :-"<<endl; //displaying the contents of v2 for(int i=0;i<v2.size();i++) cout<<v2[i]<<endl; }

OUTPUT:


EXPLANATION:

As we can observe in the output, str1 is split by taking a whitespace as the delimiter and in str2 we are using 'e' as a delimiter and splitting the string with respect to 'e'.


More Articles of Md Safi Ur Rahman Khan:

Name Views Likes
C++ boost::algorithm::string::join() 577 0
C++ boost::algorithm::string::split() 607 0
C++ boost::algorithm::string::find_all() 646 0
C++ boost::algorithm::string::erase_tail() 249 0
C++ boost::algorithm::string::replace_tail() 211 0
C++ boost::algorithm::string::erase_head() 277 0
C++ boost::algorithm::string::replace_head() 209 0
C++ boost::algorithm::string::erase_all() 895 1
C++ boost::algorithm::replace_all() 2049 0
C++ boost::algorithm::string::erase_nth() 215 0
C++ boost::algorithm::string::replace_nth() 221 0
C++ boost::algorithm::string::replace_last() 246 0
C++ boost::algorithm::string::erase_last() 243 0
C++ boost::algorithm::string::erase_first() 215 1
C++ boost::algorithm::string::replace_first() 432 0
C++ boost::algorithm::string::find_token() 351 0
C++ boost::algorithm::string::find_tail() 200 1
C++ boost::algorithm::string::find_head() 212 0
C++ boost::algorithm::string::find_last() 300 1
C++ boost::algorithm::string::find_first() 749 1
C++ boost::algorithm::string::all() 224 1
C++ boost::algorithm::string::lexicographical_compare() 214 0
C++ boost::algorithm::string::equals() 378 0
C++ boost::algorithm::string::contains() 1822 0
C++ boost::algorithm::string::ends_with() 1282 0
C++ boost::algorithm::string::starts_with() 2058 0
C++ boost::algorithm::string::trim_if() 712 1
C++ boost::algorithm::string::trim() 3796 0
C++ boost::algorithm::string::trim_right_if() 522 1
C++ boost::algorithm::string::trim_left_if() 384 1
C++ boost::algorithm::string::trim_right() 282 3
C++ boost::algorithm::string::trim_left() 363 1
C++ boost::algorithm::string::to_lower() 736 1
C++ boost::algorithm::string::to_upper() 296 1
C++ Program to Implement Dequeue 1327 5
C++ Program to Implement Dequeue 241 3

Comments