How can I check if vector elements are in order co

2019-10-02 01:08发布

问题:

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:

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 {
    // ?? 
}


回答2:

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().



回答3:

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.



回答4:

Use std::is_sorted algorithm of STL.



回答5:

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].



回答6:

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.



回答7:

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].



标签: c++ vector