C++

How to Sort Vectors Using lexicographical_compare() Function C++

The function is a set of instructions that perform a specific task. It is used to make code organized, modular, and reusable. In C++, functions play an important role to make code simple. For instance, the “lexicographical_compare()” function is used to compare two sequences or ranges lexicographically.

In this comprehensive guide, we will demonstrate the “lexicographical_compare()” function in C++ with the program.

What is the “lexicographical_compare()” Function in C++

In C++, the “lexicograpfical_compare()” function is a very effective operation to compare and sort elements in a sequence (lexicographical order). It provides the facility to determine the respective order which is the result of comparing sequences, such as ranges and strings. This function is accessible in C++ by using the “<algorithm>” header file.

Syntax

Here is the syntax of “lexicographical_compare()” function in C++:

template <class InputIter1, class InputIter2>
bool lexicographical_compare(InputIter1 first1, InputIter1 last1, InputIter2 first2, InputIter2 last2);

According to the above-provided code, the “lexicographical_compare()” function accepts two pairs of ranges the “first1” and “last1” input iterators for the first range and the “first2” and “last2” input iterators for the range second. It matches both range elements in lexicographical order.

Return Type: It returns the boolean value(true or false). It will return true when the first range is lexicographically smaller than the second range otherwise returns false.

Exceptions: If there is an error found during the comparison it will throw an exception.

How to Sort Vectors Using lexicographical_compare() Function C++

The “lexicographical_compare()” function is frequently used in sorting techniques to find out the element’s order. It matches the respective order of the elements till the range is finished.

Example of lexicograpical_compare() Function Sort and Compare the String Vectors

Let’s check out the provided example that demonstrates the “sort()” method with the “lexicographical_compare()”:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

Here:

  • <iostream>” is used for the input and output operations.

<vector>” is a C++ standard library container class and is used to hold templates that offer dynamic array capabilities.

  • <algorithm>” header file is used to access the “sort()” method for the “lexicographical_compare()” function.
  • using namespace std” is referred to as a directive that uses all the names from the namespace without explicitly mentioning the prefix “std”.

Next, inside the “main()” function, we initialized two vectors “vect1” and “vect2” with the same string. After this, used the “sort()” method to sort the elements in lexicographical order of both vectors with “vect1.begin()” and “vector1.end()” iterators range for “vect1”, and “vect2.begin()” and “vect2.end()” range for “vect2”. Then, invoked the “lexicographical_compare()” function that takes four arguments for both vectors.

The results will be saved with “bool” type in the “result” variable and return true if the “vect1” range is lexicographically smaller than the “vect2” range, and false otherwise. Lastly, apply the “if” statement to display the results. If the “result” is true it means the “vect1” is lexicographically “>” than “vect2”. Otherwise, the else condition will be executed:

int main() {
    vector<string> vect1 = {"Spring", "Summer", "Autumn", "Winter"};
    vector<string> vect2 = {"Spring", "Summer"};

    sort(vect1.begin(), vect1.end());
    sort(vect2.begin(), vect2.end());
    // compare both vectors using lexicographical_compare()
    bool result =lexicographical_compare(
        vect1.begin(), vect1.end(),
        vect2.begin(), vect2.end()
    );

    if (result) {
        cout << "vect1 is lexicographically greater than vect2" <<endl;
    } else if (lexicographical_compare(
        vect2.begin(), vect2.end(),
        vect1.begin(), vect1.end()
    )) {
        cout << "vect1 is lexicographically less than vect2" <<endl;
    } else {
        cout << "vect1 is lexicographically equal to vect2" <<endl;
    }
    return 0;
}

Output

That’s all! You have learned about the “lexicographical_compare()” function in C++.

Conclusion

In C++, the “lexicographical_compare()” function is used to sort and match the sequence of elements in lexical order. The sorting algorithm with the “lexicograpgical_compare()” method is widely implemented to find the relative order. In this tutorial, we demonstrated the “lexicographical_compare()” function in C++.

About the author

Kaynat Asif

My passion to research new technologies has brought me here to write for the LinuxHint. My major focus is to write in C, C++, and other Computer Science related fields. My aim is to share my knowledge with other people.