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

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


     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 find the first occurence of the given token in the input string. It returns an iterator_range for the aquired token as in respect to the arguments provided.

header file:






    input_string : an input string

    pred : A predicate function that is used to identify a token.

eCompress : it takes either token_compress_on or token_compress_off.

eCompress is of the type token_compress_mode_type which is essentially an enum having two possible values i.e., token_compress_on and token_compress_off. If token_compress_on then it compresses the adjacent characters also in the iterator returned in the end.( It compresses only those characters that are adjacent to the first identified token and also satisifies the predicate function specified). If token_compress_off then it only returns the first identified token that satisfies the predicate.

The predicate function maybe explicitly written by the user or m=can be any other predicate string function.

The return type is iterator_range. i.e., it returns an object of class iterator_range which can be used to traverse the returned token of the string.


#include<iostream> #include<boost/algorithm/string.hpp> #include<boost/range.hpp> #include<ctype.h> using namespace std; using namespace boost::algorithm; using namespace boost; //defining a predicate function bool func(char c) { return c=='o' || c=='e' || c=='l'; } int main() { char s1[] = "Hello and Welcome to Boost "; char s2[] = "It is very interesting 7873"; char s3[] = "srtwyuzx 0043A"; //function calls iterator_range<char*> it1 = find_token(s1 ,func,token_compress_on); iterator_range<char*> it2 = find_token(s1 ,func); iterator_range<char*> it3 = find_token(s2 , is_digit(),token_compress_on); iterator_range<char*> it4 = find_token(s2 , is_alpha(),token_compress_on); iterator_range<char*> it5 = find_token(s3 , is_xdigit(),token_compress_on); cout<<"With token_compress_on in s1 :- "<<it1<<endl; cout<<"Without token-compress_on in s1 :- "<<it2<<endl<<endl; cout<<"Identifying the first only digits token in s2 :- "<<it3<<endl; cout<<"Identifying the first only alphabets token in s2 :- "<<it4<<endl<<endl; cout<<"Identifying the first hexadecimal token in s3:- "<<it5<<endl; }



As we can observe from the above output:

When we apply the function on s1 with token_compress_on, it first identifies 'e' in 'hello' as the first token and then checks the adjacent tokens. Since 'l' and 'o' also satisfy the predicate, it returns 'ello' as the iterator. But when token_compress_off is specified(or not since it is default) it just returns 'e' since it is the first token to satisfy the predicate.

Similarly, in s2, we are applying predefined string functions like is_digit() and is_alpha() which identify digits and alphabets.

For s3 we are specifying is_xdigit() which identifies hexadecimal digits.

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() 647 0
C++ boost::algorithm::string::erase_tail() 249 0
C++ boost::algorithm::string::replace_tail() 212 0
C++ boost::algorithm::string::erase_head() 277 0
C++ boost::algorithm::string::replace_head() 210 0
C++ boost::algorithm::string::erase_all() 898 1
C++ boost::algorithm::replace_all() 2050 0
C++ boost::algorithm::string::erase_nth() 216 0
C++ boost::algorithm::string::replace_nth() 222 0
C++ boost::algorithm::string::replace_last() 247 0
C++ boost::algorithm::string::erase_last() 244 0
C++ boost::algorithm::string::erase_first() 216 1
C++ boost::algorithm::string::replace_first() 433 0
C++ boost::algorithm::string::find_token() 352 0
C++ boost::algorithm::string::find_tail() 201 1
C++ boost::algorithm::string::find_head() 213 0
C++ boost::algorithm::string::find_last() 301 1
C++ boost::algorithm::string::find_first() 749 1
C++ boost::algorithm::string::all() 225 1
C++ boost::algorithm::string::lexicographical_compare() 215 0
C++ boost::algorithm::string::equals() 378 0
C++ boost::algorithm::string::contains() 1826 0
C++ boost::algorithm::string::ends_with() 1283 0
C++ boost::algorithm::string::starts_with() 2060 0
C++ boost::algorithm::string::trim_if() 713 1
C++ boost::algorithm::string::trim() 3797 0
C++ boost::algorithm::string::trim_right_if() 522 1
C++ boost::algorithm::string::trim_left_if() 385 1
C++ boost::algorithm::string::trim_right() 282 3
C++ boost::algorithm::string::trim_left() 364 1
C++ boost::algorithm::string::to_lower() 737 1
C++ boost::algorithm::string::to_upper() 298 1
C++ Program to Implement Dequeue 1327 5
C++ Program to Implement Dequeue 241 3