We will learn about different ways to check if a vector is sorted or not in C++.

Technique 1: Using STL Algorithm std::is_sorted()

In C++, the STL Algorithm provides a function std::is_sorted() to verify of a given sequence is sorted or not. It accepts two iterators pointing to the start and end of a range, and returns true of all the elements in this range are sorted. To verify of a vector is sorted or not. We can pass the start and end iterators of vector in it. Let’s see some examples,

Example 1

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main()
{
    std::vector<int> vecObj{10, 12, 13, 78, 98, 99};

    // Check if the vector is sorted
    if (std::is_sorted(vecObj.begin(), vecObj.end()))
    {
        std::cout<<"Vector is sorted" <<std::endl;
    }
    else
    {
        std::cout<<"Vector is not sorted" <<std::endl;
    }

    return 0;
}

Output:

Vector is sorted

It confirmed that the vector of integer is sorted.

Example 2: check if vector of string is sorted

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

int main()
{
    std::vector<std::string> vecObj{"is", "this", "what", "where", "why"};

     // Check if the vector is sorted
    if (std::is_sorted(vecObj.begin(), vecObj.end()))
    {
        std::cout<<"Vector is sorted" <<std::endl;
    }
    else
    {
        std::cout<<"Vector is not sorted" <<std::endl;
    }

    return 0;
}

Output:

Vector is sorted

Example 3: Check if vector of string is sorted (case insensitive)

We can also pass custom comparator functions in is_sorted() function. With this, the is_sorted() function will use this custom comparator while comparing the elements. So, create a function that compares the strings in case insensitive manner, and pass that function to the is_sorted() function. It will check if vector of string is sorted without considering the case. Let’s see the example,

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

bool compareStrings(std::string first, std::string second)
{
    std::transform(first.begin(), first.end(), first.begin(), ::tolower);
    std::transform(second.begin(), second.end(), second.begin(), ::tolower);
    return first.compare(second) == 0;
}

int main()
{
   std::vector<std::string> vecObj{"is", "this", "What", "WHERE", "why"};

    // Check if the vector is sorted
    if (std::is_sorted(vecObj.begin(), vecObj.end(), compareStrings))
    {
        std::cout<<"Vector is sorted" <<std::endl;
    }
    else
    {
        std::cout<<"Vector is not sorted" <<std::endl;
    }

    return 0;
}

Output:

Vector is sorted

Technique 2: Basic solution using a for loop

Loop through all elements of vector, and for each element at ith index, check it it is smaller than element at index i+1.

#include <iostream>
#include <vector>
#include <string>

int main()
{
    std::vector<int> vecObj{10, 12, 13, 78, 98, 99};

    bool result = true;
    for (int i = 1; i < vecObj.size(); i++)
    {
        if (vecObj[i] < vecObj[i-1])
        {
            result = false;
            break;
        }
    }

    // Check if the vector is sorted
    if (result)
    {
        std::cout<<"Vector is sorted" <<std::endl;
    }
    else
    {
        std::cout<<"Vector is not sorted" <<std::endl;
    }
}

Conclusion

We learned about different ways to check if a vector is sorted or not in C++.

Ritika Ohri

Hi, I am Ritika Ohri, founder of this blog. I craft comprehensive programming tutorials and also manage a YouTube channel. You can also connect with me on Linkedin.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.