At the moment my solution is to iterate through the map to solve this.
I see there is a upper_bound method which can make this loop faster, but is there a quicker or more succinct way?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The end:
Maps (and sets) are sorted, so the first element is the smallest, and the last element is the largest. By default maps use
std::less, but you can switch the comparer and this would of course change the position of the largest element. (For example, usingstd::greaterwould place it atbegin().Keep in mind
rbeginreturns an iterator. To get the actual key, usem.rbegin()->first. You might wrap it up into a function for clarity, though I’m not sure if it’s worth it: