| | |
Highest Value in vector
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
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 ?
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.
•
•
Join Date: Feb 2008
Posts: 517
Reputation:
Solved Threads: 1
Thanks for the link. I have red it and found the information. I think this will do it.
C++ Syntax (Toggle Plain Text)
int HighestValue = *std::max_element( &Numbers[3], &Numbers[5] );
Last edited by Jennifer84; Sep 22nd, 2008 at 4:21 pm.
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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:
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.
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.
![]() |
Similar Threads
- Sort a std::vector<int> (C++)
- How to search for an element in a 2D vector? (C++)
- hi there!!!hw problem!! (C++)
- need urgent help with vector (C++)
- War card game (Java)
- Need some help with Student Grade program (C++)
- Not sure where to start. (C++)
- finding max value of 10 integers (C++)
Other Threads in the C++ Forum
- Previous Thread: c++ problem
- Next Thread: Debug Assertion Failure
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





