How to use the lexicographical_compare() in the C++
Hello, I am studying in the first year of the Msc. I have the basic knowledge of the C++ language. I also have the knowledge about the functions in the C++ language. But I do not know anything about the lexicographical_compare() function of C++ language. So, I would like to know about the lexicographical_compare() function. I also would like to know about the how to use the lexicographical_compare() function in the C++ language. Reply Me.
The lexicographical_compare() in the C++
Hi, The lexicographical_compare() function can be used in the C++ language to show the less than comparison lexicographically. The lexicographical_compare() function can returns the true value if the given specified range as [fst1,lst1) can be compares lexicographically less than that the range that can be [fst2,lst2). The following can be the syntax of the lexicographical_compare() function :
Code:
template <class InptItrtr1, class InptItrtr2>
bool lexicographical_compare ( InptItrtr1 fst1, InptItrtr1 lst1,
InptItrtr2 fst2, InptItrtr2 lst2 );
Use the lexicographical_compare() in the C++
The following can be the one more example on the lexicographical_compare() function that demonstrates you how the lexicographical_compare() function can be used in the C++ language :
Code:
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
bool mcmp (char d1, char d2)
{
return tolower(d1)<tolower(d2);
}
int main ()
{
char ft[]="Apple";
char sn[]="apartment";
cout << "Using default comparison (operator<): ";
if (lexicographical_compare(ft,ft+5,sn,sn+9))
cout << ft << " is less than " << sn << endl;
else
if (lexicographical_compare(sn,sn+9,ft,ft+5))
cout << ft << " is greater than " << sn << endl;
else
cout << ft << " and " << sn << " are equivalent\n";
cout << "Using mcmp as comparison object: ";
if (lexicographical_compare(ft,ft+5,sn,sn+9,mcmp))
cout << ft << " is less than " << sn << endl;
else
if (lexicographical_compare(sn,sn+9,ft,ft+5,mcmp))
cout << ft << " is greater than " << sn << endl;
else
cout << ft << " and " << sn << " are equivalent\n";
return 0;
}
Output:
Code:
Using default comparison (operator<): Apple is less than apartment
Using mcmp as comparison object: Apple is greater than apartment
How to use the lexicographical_compare() in the C++
The following can be the parameters of the lexicographical_compare() function in the C++ language :
1. fst2, lst2 : These parameters can be used in the C++ language for an input iterators to the final and starting positions of the given specified second sequence. The range used can be as [fst2,lst2).
2. fst1, lst1 : These parameters can be used in the C++ language for an input iterators to the final and starting positions of the given specified first sequence. The range used can be as [fst1,lst1) that can contains all of an elements that can be between fst1 and lst1.
The lexicographical_compare()
The lexicographical_compare() function can returns the true value if the given specified range of an elements as [strt1,nd1) can be lexicographically less than the given range of an elements as [strt2,nd2). If you can be confused about what a lexicographic means then the a lexicographic means lexicographic can might help you to know that the dictionaries can be ordered lexicographically. The lexicographical_compare() function can be runs in a linear time.
Re: How to use the lexicographical_compare() in the C++
The code of lines that can be mentioned below gives you the knowledge about the lexicographical_compare() function :
Code:
using std::lexicographical_compare;
int main()
{
int B1[] = {3, 1, 4, 1, 5, 9, 3};
int B2[] = {3, 1, 4, 2, 8, 5, 7};
int B3[] = {1, 2, 3, 4};
int B4[] = {1, 2, 3, 4, 5};
const int M1 = sizeof(B1) / sizeof(int);
const int M2 = sizeof(B2) / sizeof(int);
const int M3 = sizeof(B3) / sizeof(int);
const int M4 = sizeof(B4) / sizeof(int);
bool D12 = lexicographical_compare(B1, B1 + M1, B2, B2 + M2);
bool D34 = lexicographical_compare(B3, B3 + M3, B4, B4 + M4);
cout << "B1[] < B2[]: " << (D12 ? "true" : "false") << endl;
cout << "B3[] < B4[]: " << (D34 ? "true" : "false") << endl;
}
int main()
{
bool D12 = std::lexicographical_compare(B1, B1 + M1, B2, B2 + M2);
bool D34 = std::lexicographical_compare(B3, B3 + M3, B4, B4 + M4);
}