Uwpbeginner
Uwpbeginner

Reputation: 405

Find maximum element from a vector for a given range

I have tried to use the max function but it needs a iterator and that are A.begin and A.end but for my program I want to find for a range say from i to x .I tried to read the docs but was unable to find the solution. Any help would be appreciated.Thank you.

Upvotes: 3

Views: 6469

Answers (1)

The Quantum Physicist
The Quantum Physicist

Reputation: 26256

You're not looking for max. But for std::max_element.

To use it:

std::vector<int> v;
// fill it
auto max_it = std::max_element(v.begin()+i, v.end());

And to check in the range [i,j):

auto max_it = std::max_element(v.begin()+i, v.begin()+j);

max_it here is an iterator, to get the number from it:

int max_number = *max_it;

Upvotes: 9

Related Questions