457,704 Members | 1,270 Online Need help? Post your question and get tips & solutions from a community of 457,704 IT Pros & Developers. It's quick & easy.

# Retrieving the index of the maximum value in a vector

3 Replies

 Expert 100+ P: 1,510 Hello I have the following task but am battling with the final output. How do I keep two different vectors in sync and how would I retrieve the index for the maximum value of one of the vectors?? (using Dev-C++ compiler) M the function call Expand|Select|Wrap|Line Numbers *max_element (tempvec.begin(),tempvec.end())   returns an iterator to the maximum element - to get the associated index you can use the distance() function http://www.sgi.com/tech/stl/distance.html e.g. to print the time associated with the maximum temperature Expand|Select|Wrap|Line Numbers << "Maximum temperature was " << *max_element (tempvec.begin(),tempvec.end()) << "   at time " << timevec.at(distance(tempvec.begin(), max_element (tempvec.begin(),tempvec.end())))<< " ?????? " << endl;   the expression Expand|Select|Wrap|Line Numbers distance(tempvec.begin(), max_element (tempvec.begin(),tempvec.end())   returns the distance between the begining of the vector and the iterator to the maximum Jan 3 '07 #2

 P: 23 Thank you Horace! This site rocks! Much appreciated. Cheers M Jan 3 '07 #3

 P: 38 Some more points: hours, minutes, and seconds can be calculated like this: int time_in_seconds; int hours = time_in_seconds / 3600; int minutes = (time_in_seconds / 60) % 60; int seconds = time_in_seconds % 60; system("PAUSE") is unportable; don't use it. You might want to try a vector of std::pairs, rather than managing two vectors. You would have to define your own comparison function, though. Jan 4 '07 #4 