Highest Value in vector

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Highest Value in vector

 
0
  #1
Sep 22nd, 2008
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
Last edited by Jennifer84; Sep 22nd, 2008 at 3:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Highest Value in vector

 
0
  #2
Sep 22nd, 2008
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 517
Reputation: Jennifer84 is an unknown quantity at this point 
Solved Threads: 1
Jennifer84 Jennifer84 is offline Offline
Posting Pro

Re: Highest Value in vector

 
0
  #3
Sep 22nd, 2008
Thanks for the link. I have red it and found the information. I think this will do it.

  1. int HighestValue = *std::max_element( &Numbers[3], &Numbers[5] );


Originally Posted by stilllearning View Post
See http://www.cppreference.com/wiki/stl...hm/max_element
Last edited by Jennifer84; Sep 22nd, 2008 at 4:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Highest Value in vector

 
0
  #4
Sep 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC