Answers for "how to check if vector is ordered c++"

C++
0

how to check if vector is ordered c++

#include <vector> // vector 
#include <algorithm> // is_sorted
#include <iostream> // cout 
using namespace std;
int main()
{
    vector<int> a = {6,5,3,5,7,8,5,2,1};
    vector<int> b = {1,2,3,4,5,6,7,8,9};
    
    if(is_sorted(a.begin(), a.end()))
        cout << "a is sorted\n";
    else
        cout << "a is not sorted\n";
    
    if(is_sorted(b.begin(), b.end()))
        cout << "b is sorted\n";
    else
        cout << "b is not sorted\n";
}
Posted by: Guest on August-21-2021

Browse Popular Code Answers by Language