473,387 Members | 1,541 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,387 software developers and data experts.

stl map, lower_bound and upper_bound

I have an application which uses maps.
I need to find elements in the map, without knowing the exact key value.
I thought I could use the lower_bound, or upper_bound function for this purpose.
However, after searching the internet and after some experiments with a few compilers
I am completely confused.
I find several contradictory descriptions of these functions. Also the results from test
programs are not clear to me.
So, maybe someone can help me, with the following example.
Suppose I have a map with keys of type int.
This map is populated with three elements, with key values 1, 3 and 7.
Which values should be returned by lower_bound and upper_bound when these are called
with parameter values 0, 1, 2, 3, 4, 5, 6, 7, 8?
Can someone fill in this table:

Parameter value 0 1 2 3 4 5 6 7 8
result of lower_bound
result of upper_bound

Thanks,
Fred.Zwarts.
Apr 20 '06 #1
5 8546

"Fred Zwarts" <F.******@KVI.nl> skrev i meddelandet
news:e2**********@info.service.rug.nl...
I have an application which uses maps.
I need to find elements in the map, without knowing the exact key value.
I thought I could use the lower_bound, or upper_bound function for this purpose. However, after searching the internet and after some experiments with a few compilers I am completely confused.
I find several contradictory descriptions of these functions. Also the results from test programs are not clear to me. So, maybe someone can help me, with the following example.
Suppose I have a map with keys of type int.
This map is populated with three elements, with key values 1, 3 and 7.


Are you sure it is a std::map you want to use and not a std::set?
A map is a map between key<->value pairs.
A set is just a set of (unique) keys.

Regards,

Peter Jansson
http://www.p-jansson.com/
http://www.jansson.net/
Apr 20 '06 #2
Hello,
Look at this page
http://www.sgi.com/tech/stl/upper_bound.html#1

Apr 20 '06 #3
Fred Zwarts wrote:
I have an application which uses maps.
I need to find elements in the map, without knowing the exact key value.
I thought I could use the lower_bound, or upper_bound function for this
purpose.
They are not designed for that.
However, after searching the internet and after some experiments
with a few compilers I am completely confused.
lower_bound gives you the first element with the specified key. If there is
none, the first element with a key greater than the specified one is
returned. If there is none of those either, the 'last' argument is
returned.
upper_bound gives you the first element that has a key greater than the one
you specified or 'last' if none is found.
I find several contradictory descriptions of these functions. Also the
results from test programs are not clear to me.
So, maybe someone can help me, with the following example.
Suppose I have a map with keys of type int.
This map is populated with three elements, with key values 1, 3 and 7.
Which values should be returned by lower_bound and upper_bound when these
are called with parameter values 0, 1, 2, 3, 4, 5, 6, 7, 8?
Can someone fill in this table:


Parameter value 0 1 2 3 4 5 6 7 8
result of lower_bound 1 1 3 3 7 7 7 7 last
result of upper_bound 3 3 3 7 7 7 7 last last

Apr 20 '06 #4
Fred Zwarts wrote:
I have an application which uses maps.
I need to find elements in the map, without knowing the exact key value.
I thought I could use the lower_bound, or upper_bound function for this
purpose.
They are not designed for that.
However, after searching the internet and after some experiments
with a few compilers I am completely confused.
lower_bound gives you the first element with the specified key. If there is
none, the first element with a key greater than the specified one is
returned. If there is none of those either, end() is returned.
upper_bound gives you the first element that has a key greater than the one
you specified or end() if none is found.
I find several contradictory descriptions of these functions. Also the
results from test programs are not clear to me.
So, maybe someone can help me, with the following example.
Suppose I have a map with keys of type int.
This map is populated with three elements, with key values 1, 3 and 7.
Which values should be returned by lower_bound and upper_bound when these
are called with parameter values 0, 1, 2, 3, 4, 5, 6, 7, 8?
Can someone fill in this table:


Parameter value 0 1 2 3 4 5 6 7 8
result of lower_bound 1 1 3 3 7 7 7 7 end()
result of upper_bound 3 3 3 7 7 7 7 end() end()

Apr 20 '06 #5
"Fred Zwarts" <F.******@KVI.nl> wrote in message
news:e2**********@info.service.rug.nl...
I have an application which uses maps.
I need to find elements in the map, without knowing the exact key value.
I thought I could use the lower_bound, or upper_bound function for this
purpose.
However, after searching the internet and after some experiments with a
few compilers
I am completely confused.
I find several contradictory descriptions of these functions. Also the
results from test
programs are not clear to me.
So, maybe someone can help me, with the following example.


I don't think your example will help you solve your problem.

Here's a capsule summary of what happens.

If the element you seek is in the container, then lower_bound returns an
iterator that refers to that element, and upper_bound returns an iterator
that refers to the next element (or one past the end if there is no such
element).

If the element is not in the container, then lower_bound and upper_bound
both return the same value, namely an iterator referring to the first
element *after* the one you seek (or one past the end if there is no such
element).

So if the map keys are strings, and you know the first few characters of the
string, you could use lower_bound to look for the given key.

If the result is one past the end of the container, you know that no element
begins with your key. Otherwise, the iterator gives you a place to start
looking.

For example, if your key is "foo", lower_bound will return the first element
that starts with "foo", if such an element exists. If not, it will return
either the first element that is > "foo" or one past the end if no such
element exists.
Apr 23 '06 #6

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

Similar topics

4
by: Alex Vinokur | last post by:
============================= Windows 2000 MinGW 2.0.0.-2 GNU gcc/g++ version 3.2 ============================= I have some question concerning the lower_bound algorithm. ========= C++ code...
1
by: Jim West | last post by:
The following code does what I want it to do on two different compilers (g++ and Intel icc under Linux), but I'm not sure if it is undefined and just happens to work: class A { public: int...
3
by: Jaap | last post by:
Hi all, I'm looking for functionality very similar to std::map::lower_bound: I'd like to find the first element whose key is equal to or smaller than the key I pass. In a map filled like...
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: Allerdyce.John | last post by:
On page 181 of Effective STL, it said 'It 's knowning when equal_range is a better way to search than lower_bound, knowing when lower bound is preferable to find..." My question is understand...
3
by: Diego Martins | last post by:
Andrew Koenig wrote:
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...
1
by: phdscholar80 | last post by:
I am using the following code: #include <vector> #include <algorithm> class A { }; bool comparator( A * b, const char * a )
0
by: mathieu | last post by:
hi there, I do not understand why the following piece of code is not working for me. I have a set of cstrings, where I am trying to find one 'UT'. If I search from "AE" to "OB or OW" everything...
4
by: qlin88 | last post by:
Hi, In STL multi-map, the lower_bound, upper_bound,equal_range all return an iterator. Now ifone wants to iterate from an upper bound towards a lower bound, what would be the best way to do it?...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.