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














































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



lexicographical_compare():

     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 check whether the given first string is lexicographically smaller than the second or not. i.e., checks from the first character whether the first string's character are smaller than the corresponding characters in the second string. This function is an overload of std::lexicographical_compare.

header file:

boost/algorithm/string.hpp

syntax:

     lexicographical_compare(string1,string2);

parameters:

    string1 : a first string

    string2 : a second string

(along with the above parameters an additional optional parameter can be passed which specifies the predicate for comparision. i.e., the user may specify his own new function for comparision.

The given function is case sensitive, i.e., it does the check case-sensitively. For example, "I am not well" is not same as "i am not WELL"

 There is another variation present i.e., ilexicographical_compare() , which takes the same parameters as input, but instead of doing a case-sensitive test, it ignores the case of the characters and does the check, i.e., it does the check case-insensitively. For example, "I am not well" is same as "i am not WELL".

SAMPLE CODE:

#include<iostream> #include<boost/algorithm/string.hpp> using namespace std; using namespace boost::algorithm; int main() { //Input strings string str[] = {"Welcome","Hello","its very interestING ","It is very effective"}; //Test strings string test[] = {"Welcome","Hi","it is very interestING ","jt is very interesting"}; //performing the checks and printing the results with lexicographical_compare() cout<<"Using lexicographical_compare()"<<endl<<endl; for(int i=0;i<4;i++) { cout<<str[i]<<" is checked with "<<test[i]<<endl; cout<<"Result : The string1 is less than string2:- "<<lexicographical_compare(str[i],test[i])<<endl<<endl; } }

OUTPUT:

EXPLANATION:

In the above output, 1 means that string1 is smaller than string2. 0 means that string1 is larger than or equal to string2.

As we can observe for str2 and str4 lexicographical_compare() returns 1 and for str3 and str1 it returns 0 as string2 is lexicographically lesser than string1.(lexicographical_compare() performs case-sensitve check).



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() 351 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

Comments