I have put values into 6 elements of the vector Numbers like below.
What I wonder is if there is any function to check for the Highest value
for a given range of the vector.
Say I want to find the highest value from elements [3] to [5] wich in this case then will be: 6
I know I could use a loop to find this value but are there any built in method for something like this ?

std::vector<int> Numbers(5);

Numbers[0] = 1;
Numbers[1] = 2;
Numbers[2] = 3;
Numbers[3] = 4;
Numbers[4] = 5;
Numbers[5] = 6; //This has the highest value for ex: element 3 - 5

Recommended Answers

All 3 Replies

the max_element prototype expects the arguments to be of the form iterator and returns an iterator.

so you may need something of this type:

std::vector<int>::iterator myIter;
myIter = max_element(Numbers.begin(),Numbers.begin() + 2);

This will give you the maximum value between the the indexes 0 and 2.
Also I noticed that even though you have declared your vector to be of size 5, you are trying to add 6 elements to it ? You may want to fix that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.