Problem with map upper_bound

H

Henrik Goldman

Hello,

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.

Maybe I should use some combination with lower_bound instead?

-- Henrik
 
G

Greg Herlihy

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.

Greg
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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.

I'd just like to point out (though it might be obvious) that if the
latter date will always be a future date then you can always just use
end() directly and not use the more time consuming upper_bound().
 
J

James Kanze

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.

But of course, he probably doesn't want an iterator to the last
element. If he wants to find all values in the range, he is
probably better off using the half open interval returned by
lower_bound and upper_bound. (The functions have been designed
as they were for a reason.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top