Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program to Implement LexicoGraphical_Compare in STL
The C++ function std::algorithm::lexicographical_compare() tests whether one range is lexicographically less than another or not. A lexicographical comparison is the kind of comparison generally used to sort words alphabetically in dictionaries.
Declaration
template <class InputIterator1, class InputIterator2>
bool lexicographical_compare(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2);
Algorithm
Begin result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end()) if (result == true) Print v1 is less than v2 result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end()) if (result == false) Print v1 is not less than v2 End
Example
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main(void) {
//initialization of v1 and v2
vector<string> v1 = {"One", "Two", "Three"};
vector<string> v2 = {"one", "two", "three"};
bool result;
result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
if (result == true)
cout << "v1 is less than v2." << endl;
v1[0] = "two";
result = lexicographical_compare(v1.begin(), v1.end(), v2.begin(), v2.end());
if (result == false)
cout << "v1 is not less than v2." << endl;
return 0;
}
Output
v1 is less than v2. v1 is not less than v2.
Advertisements