Users Online
· Guests Online: 103
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
lexicographical_compare() in C++
lexicographical_compare() in C++
This C++ program demonstrates the lexicographical_compare() algorithm. The lexicographical_compare function takes five parameters – the four parameters are begin and end iterators to the two containers and the fifth parameter is a predicate. The predicate we are using is equal_to which is defined in the functional library.
Here is the source code of the C++ program which demonstrates the lexicographical_compare() algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
-
/*
-
* C++ Program to demonstrate the lexicographical_compare() algorithm
-
*/
-
#include <iostream>
-
#include <algorithm>
-
#include <vector>
-
#include <functional>
-
-
void print(const std::vector <int>& v)
-
{
-
std::vector <int>::const_iterator i;
-
for(i = v.begin(); i != v.end(); i++)
-
{
-
std::cout << *i << " ";
-
}
-
std::cout << std::endl;
-
}
-
-
int main()
-
{
-
std::vector <int> a(10), b(10);
-
bool result;
-
-
for (int i = 0; i < 10 ;i++)
-
{
-
a[i] = i + 1;
-
b[i] = i + 1;
-
}
-
std::cout << "a : ";
-
print(a);
-
std::cout << "b : ";
-
print(b);
-
// Comparing a and b lexicographically
-
result = std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), std::equal_to <int>());
-
if(result == true)
-
{
-
std::cout << "a is lexicographically equal to b."
-
<< std::endl;
-
}
-
else
-
{
-
std::cout << "a is lexicographically not equal to b."
-
<< std::endl;
-
}
-
}
$ a.out a : 1 2 3 4 5 6 7 8 9 10 b : 1 2 3 4 5 6 7 8 9 10 a is lexicographically equal to b.
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.