-5

I need to check if in my vector the elements are in order consecutively?

for(i=1; i<=K; i++)
    if(v[i]=v[i+1]-1)

If the statement would be true I want to return the biggest integer.

ex. 4 5 6 7

7
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
supreme_tg
  • 113
  • 1
  • 1
  • 6

7 Answers7

15

There's an algorithm for that: std::is_sorted:

if (std::is_sorted(v.begin(), v.end()) {
    return v.back(); // the largest element would be the last one
}
else {
    // ?? 
}
Barry
  • 286,269
  • 29
  • 621
  • 977
4

I need to check if in my vector the elements are in order

Use C++11's std::is_sorted algorithm:

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

int main()
{
    std::vector<int> v1 { 4, 5, 7, 6 };
    std::vector<int> v2 { 4, 5, 6, 7 };

    using std::begin;
    using std::end;

    std::cout << std::is_sorted(begin(v1), end(v1)) << "\n";
    std::cout << std::is_sorted(begin(v2), end(v2)) << "\n";
}

If the statement would be true I want to return the biggest integer.

That's a job for std::max_element:

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

int main()
{
    std::vector<int> v1 { 4, 5, 7, 6 };
    std::vector<int> v2 { 4, 5, 6, 7 };

    using std::begin;
    using std::end;

    std::cout << *std::max_element(begin(v1), end(v1)) << "\n";
    std::cout << *std::max_element(begin(v2), end(v2)) << "\n";
}

Note that std::max_element does not require its input to be sorted.

Or if it's sorted anyway, just use v1.back().

Christian Hackl
  • 27,051
  • 3
  • 32
  • 62
2

I would use:

is_sorted(begin(v), end(v));

If the sequence is not sorted then use:

max_element(begin(v), end(v));

to get the max.

Jorge González Lorenzo
  • 1,722
  • 1
  • 19
  • 28
2

Use std::is_sorted algorithm of STL.

Asha
  • 11,002
  • 6
  • 44
  • 66
1
bool test(vector<int> v) {
    for(int i = 0; i < v.size()-1; i++)
        if(v[i] > v[i+1])
            return false;
    return true;
}

If the vector is in order, so the biggest is the last one, that is, v[v.size()-1].

Paulo
  • 1,458
  • 2
  • 12
  • 26
1

From the question it seems you are interested if the vector elements are consective integers. If that is the case you can use a flag to indicate if the condition holds and do a single iteration:

bool consecutive = false;
for (int i = 1; i < v.size(); ++i) {
  if (v[i] != v[i - 1] + 1) {
    consecutive = false;
    break;
  }
}
if (consecutive) {
   cout << v.back() << endl; // the last element is the biggest.
}

However your question title suggests you are interested in checking if the vector is sorted. If that is the case you can use the built-in function std::is_sorted that is present since c++11.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
1

Traverse through the vector starting from the second element and keep a flag variable initialized to 0, for each iteration check the following condition :-

if(v[i]<v[i-1])

if this condition is true, then set the flag to 1 and break out of the loop. After the control moves out of the loop, check the value of flag. If flag is 1, then the vector elements are not in order. Otherwise, if the flag is 0, then the vector elements are in order and you have to print the largest element which is v[v.size()-1].

ncoder
  • 151
  • 1
  • 8