473,401 Members | 2,068 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,401 software developers and data experts.

map in stl

I'm using a map for storing information.
With lower_bound i can find keys equal to or greater then the key i give as
a parameter. With upper_bound i can find keys greater then the key i give as
a parameter.

What i want is find keys less then or equal to the key i give. Is that
possible with a map? After googling for 1 hour, it seems that nobody ever
needed this functionality :-)

--

Pascal


Nov 8 '07 #1
6 1480
On Nov 8, 11:02 am, "Pascal" <NoS...@NoSpam.NoSpamwrote:
I'm using a map for storing information.
With lower_bound i can find keys equal to or greater then the key i give as
a parameter. With upper_bound i can find keys greater then the key i give as
a parameter.

What i want is find keys less then or equal to the key i give. Is that
possible with a map? After googling for 1 hour, it seems that nobody ever
needed this functionality :-)

--

Pascal
Do no know how useful would be, but how about a function like so:

// M must be sorted.
template<typename M>
typename M::const_iterator less_than_or_equal( const M& m, const
typename M::key_type& k )
{
M::const_iterator i = m.find( k );

return ( i == m.end() ? i : m.begin() );
}

Regards.

Nov 8 '07 #2
On Nov 8, 11:02 am, "Pascal" <NoS...@NoSpam.NoSpamwrote:
I'm using a map for storing information.
With lower_bound i can find keys equal to or greater then the key i give as
a parameter. With upper_bound i can find keys greater then the key i give as
a parameter.

What i want is find keys less then or equal to the key i give. Is that
possible with a map? After googling for 1 hour, it seems that nobody ever
needed this functionality :-)

--

Pascal
Or more likely:

template<typename M>
typename M::const_iterator less_than_or_equal( const M& m, const
typename M::key_type& k )
{
M::const_iterator i1 = m.find( k );

if( i1 != m.end() )
return i1;

M::const_iterator i2 = m.lower_bound( k );

return ( i2 == m.end() ? i2 : ( i2 == m.begin() ? m.end() : --i2 ) );
}

Regards.

Nov 8 '07 #3
On 2007-11-08 05:02:48 -0500, "Pascal" <No****@NoSpam.NoSpamsaid:
I'm using a map for storing information.
With lower_bound i can find keys equal to or greater then the key i give as
a parameter. With upper_bound i can find keys greater then the key i give as
a parameter.

What i want is find keys less then or equal to the key i give. Is that
possible with a map? After googling for 1 hour, it seems that nobody ever
needed this functionality :-)
Remember, iterators work in pairs: [first, last) is a sequence of
elements. When you call upper_bound you get an iterator 'res' that
points at the first element in [first, last) whose key is greater than
the key you passed. So the sequence [res, last) is the sequence of
elements with keys that are greater than the key. Now think of 'res' as
the end instead of the subsequence instead of the beginning: the
sequence [first, res) is all the elements with keys that are less than
or equal to the given key.

Similarly, if you call lower_bound, its result gives you a subsequence
[first, res) whose elements all have keys that are less than the given
key.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 8 '07 #4
Pascal wrote:
I'm using a map for storing information.
With lower_bound i can find keys equal to or greater then the key i give as
a parameter. With upper_bound i can find keys greater then the key i give as
a parameter.

What i want is find keys less then or equal to the key i give. Is that
possible with a map? After googling for 1 hour, it seems that nobody ever
needed this functionality :-)
Almost always these sorts of queries can be solved by using UB, LB, or
at times, the first increment or decrement of those. Here it's actually
very simple: you want the range [m.begin(), m.upper_bound(k)).

You should convince yourself that this does the right thing even if all
keys are greater than k or all keys are less than or equal to k. And
keep in mind the convention that ranges are specified as [start, one
past the end).

-Mark
Nov 8 '07 #5
To do what you want, loop from map.begin() to end() and break when
iterator->first searchKey.

See http://theschmitzer.blogspot.com for alternative search strategies
using predicates.

Jeff

Nov 15 '07 #6
je***************@gmail.com wrote:
To do what you want, loop from map.begin() to end() and break when
iterator->first searchKey.

See http://theschmitzer.blogspot.com for alternative search strategies
using predicates.

Jeff
Besides omitting context and replying to the wrong post, this is also
pretty bad advice. The *_bound functions are O(log n)-- reading through
the entire map is O(n). It's perfectly reasonable that one may way to
find the range in question without actually examining every element in
that range.

Regarding your blog post about partial match searching in a map, I
believe there are better ways to do this than burdening every object of
type Key with a mode flag. One possibility is to use a dynamic functor
as your comparison-- the functor maintains a pointer to some other
object whose state dictates the behavior of the functor. This allows
for a flexible comparison function without touching the Key objects.

Mark
Nov 15 '07 #7

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

Similar topics

3
by: William C. White | last post by:
Does anyone know of a way to use PHP /w Authorize.net AIM without using cURL? Our website is hosted on a shared drive and the webhost company doesn't installed additional software (such as cURL)...
2
by: Albert Ahtenberg | last post by:
Hello, I don't know if it is only me but I was sure that header("Location:url") redirects the browser instantly to URL, or at least stops the execution of the code. But appearantely it continues...
3
by: James | last post by:
Hi, I have a form with 2 fields. 'A' 'B' The user completes one of the fields and the form is submitted. On the results page I want to run a query, but this will change subject to which...
0
by: Ollivier Robert | last post by:
Hello, I'm trying to link PHP with Oracle 9.2.0/OCI8 with gcc 3.2.3 on a Solaris9 system. The link succeeds but everytime I try to run php, I get a SEGV from inside the libcnltsh.so library. ...
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: 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: 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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.