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

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


     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 last occurance of the given pattern in the input string. It starts from the first character of the string and searches for the pattern. Once it finds the last present pattern, it returns an iterator_range for the rest of the string (pattern inclusive).

header file:






    input_string : an input string

    pattern : The patterns to be searched

The return type is iterator_range. i.e., it returns an object fof class iterator_range which can be used to traverse the rest of the string pattern inclusive.


#include<iostream> #include<boost/algorithm/string.hpp> #include<boost/range.hpp> using namespace std; using namespace boost::algorithm; using namespace boost; int main() { //Declaring strings char s1[] = "abd abd bbs abd gghht"; char s2[] = "df ssd gg te er"; char s3[] = "hhehr jjjt jeke jjtjjt w"; iterator_range<char*> it1 = find_last(s1,"abd"); iterator_range<char*> it2 = find_last(s2,"te"); iterator_range<char*> it3 = find_last(s3,"jj"); //These iterators can also act as a boolean conditions if(it1) cout<<it1.begin()<<endl; if(it2) cout<<it2.begin()<<endl; if(it3) cout<<it3.begin()<<endl; //using the function directly as a boolean condition if(find_last("sddjdbkhk","hello")) cout<<"hello is present"<<endl; else cout<<"hello not present"<<endl; }



As we can observe from the above output, the iterator can be used as boolean condition. i.e., iterator_range is bool convertible. After applying find_last() it returns an iterator to traverse the rest of the string. But for s3, since "hello" is not found, the corresponding it condition fails and the else block is executed.

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() 896 1
C++ boost::algorithm::replace_all() 2050 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() 244 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() 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() 214 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() 2059 0
C++ boost::algorithm::string::trim_if() 712 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