Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find maximum element from a vector for a given range

Tags:

c++

stl

vector

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.

like image 365
Uwpbeginner Avatar asked Dec 12 '25 05:12

Uwpbeginner


1 Answers

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;
like image 160
The Quantum Physicist Avatar answered Dec 14 '25 19:12

The Quantum Physicist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!