3

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.

4
  • 5
    vector provides random access iterators so you can easily convert index to iterator items.begin() + i Commented Jul 20, 2017 at 9:20
  • It would be better if you could provide some real code Commented Jul 20, 2017 at 9:20
  • 1
    That's the good thing about all the standard algorithm functions, they take a range. Iterators doesn't have to be just the beginning and end, it can be anything in between. Commented Jul 20, 2017 at 9:21
  • Possible duplicate of Can I increment an iterator by just adding a number? Commented Jul 20, 2017 at 9:22

1 Answer 1

9

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; 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.