On 7/5/07 12:52 PM, in article 468d4c24$0$887$edfadb0f@dread12.news.tele.dk,
"Henrik Goldman" <henrik_goldman@mail.tele.dk> wrote:
Assume we have a std::map<time_t, int>.
Now I would like to find all values (second) which fits in (within the time
range of first) between first of April 1st. and end of July. Notice that we
did not yet reach end of July yet.
Using lower_bound I can easily find the first data value within this range.
However with upper_bound I get an invalid iterator when trying to get a
future data. Ideally I would like the last record which has the highest time
value.
std::map's upper_bound() method returns an iterator to the first element
whose key is greater than the specified search key (or the end() iterator if
no such element exists in the map).
So, provided that upper_bound() does not return an iterator equal to begin()
(meaning that all elements in the map are greater than the search key), then
simply decrementing the iterator that upper_bound() returns - will give you
an iterator to the last element in the map whose key value is less than or
equal to the specified search key.
end() directly and not use the more time consuming upper_bound().