This C++ program demonstrates the binary negater with less predicate. The program creates two vectors and lexicographically compares the two vectors with negated version of less predicate. This results in an inverse effect as without negation.
Here is the source code of the C++ program which demonstrates the binary negater with less predicate. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to demonstrate binary negater with less predicate
*/
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <iomanip>
using namespace std;
void print(const vector <int>& v)
{
vector <int>::const_iterator i;
for(i = v.begin(); i != v.end(); i++)
{
cout << setw(2) << *i << " ";
}
cout << endl;
}
int main()
{
vector <int> a(10), b(10);
bool result;
for (int i = 0; i < 10 ;i++)
{
a[i] = i + 1;
b[i] = i + 2;
}
cout << "a : ";
print(a);
cout << "b : ";
print(b);
// Comparing a and b lexicographically
cout << "Comparing using negater with less predicate"
<< endl;
result = lexicographical_compare(a.begin(), a.end(), b.begin(), b.end(), not2(less<int>()));
if(result == true)
{
cout << "a is lexicographically smaller than b."
<< endl;
}
else
{
cout << "a is lexicographically greater than or equal to b."
<< endl;
}
}
$ a.out a : 1 2 3 4 5 6 7 8 9 10 b : 2 3 4 5 6 7 8 9 10 11 Comparing using negater with less predicate a is lexicographically smaller than b.
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Computer Science MCQs
- Check C++ Books
- Practice Programming MCQs
- Apply for C++ Internship
- Check Programming Books