473,385 Members | 1,593 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with map upper_bound

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
Jul 5 '07 #1
3 3289
On 7/5/07 12:52 PM, in article 46*********************@dread12.news.tele.dk,
"Henrik Goldman" <he************@mail.tele.dkwrote:
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

Jul 5 '07 #2
On 2007-07-06 00:06, Greg Herlihy wrote:
On 7/5/07 12:52 PM, in article 46*********************@dread12.news.tele.dk,
"Henrik Goldman" <he************@mail.tele.dkwrote:
>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.
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().

--
Erik Wikström
Jul 6 '07 #3
On Jul 6, 12:06 am, Greg Herlihy <gre...@pacbell.netwrote:
On 7/5/07 12:52 PM, in article 468d4c24$0$887$edfad...@dread12.news.tele.dk,
"Henrik Goldman" <henrik_gold...@mail.tele.dkwrote:
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 highesttime
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() iteratorif
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 6 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

41
by: AngleWyrm | last post by:
I have created a new container class, called the hat. It provides random selection of user objects, both with and without replacement, with non-uniform probabilities. Uniform probabilities are a...
0
by: Erik Arner | last post by:
Hi, let's say I have a std::map<std::string,int> and I want to search the map for all keys that start with "foo". The regexp equivalent is to search for "foo*", or perhaps "^foo*". At present...
7
by: Chris Gordon-Smith | last post by:
I have a simulation program that calls the rand() function at various points while it executes. For the user interface that displays statistics etc. while the program runs, I use the Lazarus GUI...
2
by: jesper | last post by:
In frustration I made this code using equal_range. It does not behave in the way I expected however. It finds the correct breakpoint in the set but finds it as the first iterator. why? I would...
3
by: asdf | last post by:
Can you create std::set::upper_bound function just using std::set::lower_bound ?
3
by: Diego Martins | last post by:
Andrew Koenig wrote:
13
by: Gaijinco | last post by:
For some time now I have forced myself to use size_t instead of int when I'm using a loop to go thru an array. But today I found something odd: long hex2long(const string& s) { string...
3
by: could.net | last post by:
I have a class board, like this: struct board { int x1, x2, h; int time1, time2; void calculate_time(); void assign(int x1__,int x2__,int h__); }; And I have n boards stored in an array...
3
by: orzeech | last post by:
Hi everyone! I have a following problem: #include<iostream> #include<set> using namespace std;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.