Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding an int key less than or equal to

0 views
Skip to first unread message

DerekBaker

unread,
Oct 30, 2009, 6:41:59 PM10/30/09
to
For a map of int, is it possible to use an STL algorithm to find a key that is
less than or equal to
a value, that itself may be absent?

If not, no need to provide code: can write the loop myself.
--
Derek

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

tohava

unread,
Oct 31, 2009, 7:18:29 PM10/31/09
to
On Oct 31, 12:41 am, DerekBaker <m...@DontBother.com> wrote:
> For a map of int, is it possible to use an STL algorithm to find a key that is
> less than or equal to
> a value, that itself may be absent?

Check the methods lower_bound and upper_bound.


--

Jeff Flinn

unread,
Oct 31, 2009, 7:18:24 PM10/31/09
to
DerekBaker wrote:
> For a map of int, is it possible to use an STL algorithm to find a key
> that is less than or equal to
> a value, that itself may be absent?
>
> If not, no need to provide code: can write the loop myself.

Do you mean std::map<...>::lower_bound?

Jeff

--

Paul Bibbings

unread,
Oct 31, 2009, 7:17:58 PM10/31/09
to
DerekBaker wrote:
> For a map of int, is it possible to use an STL algorithm to find a key
> that is less than or equal to
> a value, that itself may be absent?

>From the wording of your question I am unclear as to exactly what you intend by
a "map of int." A map is formed using key/value (or rather, key/mapped type)
pairs. Are the keys ints, the corresponding mapped values, or both?

Given this uncertainty on my part, the following is at least a compilable
example that presents one idea for finding keys that are less than a given
'value' (in the general sense) that may not be present in the map. It uses a
map<int, string> by way of example, and may or may not be helpful to you in
illustrating the kinds of things that are possible using the STL alone, which is
what you are specifically asking. It finds all the mapped values corresponding
to keys (int) that are less than 6 (which is not present as a key in the map).

#include <iostream>
#include <map>
#include <string>
#include <algorithm>
#include <functional>

int main()
{
std::map<int, std::string> odds;

odds[1] = "one";
odds[3] = "three";
odds[5] = "five";
odds[7] = "seven";
odds[9] = "nine";

std::map<int, std::string>::iterator map_iter =
std::find_if(
odds.begin(),
odds.end(),
std::bind2nd(std::less<std::pair<int, std::string> >(),
std::make_pair(6, ""))
);
while (map_iter != odds.end())
{
std::cout << map_iter->second << '\n';
map_iter = std::find_if(
++map_iter,
odds.end(),
std::bind2nd(std::less<std::pair<int, std::string> >(),
std::make_pair(6, ""))
);
}

return 0;
}

Output:
one
three
five

Better, however, would be to avoid the awkwardness in the above example of
having to compare /pairs/ through providing your own functor, allowing
comparison of keys directly. You might define this as something like:

template<typename Key, typename Value>
class key_less_than {
public:
explicit key_less_than(Key key_comp)
: key_comp_(key_comp)
{ }
bool operator()(const typename std::map<Key, Value>::value_type& key_val)
{
return key_val.first < key_comp_;
}
private:
Key key_comp_;
};

and use it as:

std::map<int, std::string>::iterator map_iter =
std::find_if(odds.begin(),
odds.end(),
key_less_than<int, std::string>(6));

Regards

Paul Bibbings

--

Anand Hariharan

unread,
Oct 31, 2009, 7:18:37 PM10/31/09
to
On Oct 30, 5:41 pm, DerekBaker <m...@DontBother.com> wrote:
> For a map of int, is it possible to use an STL algorithm to find a key that is
> less than or equal to
> a value, that itself may be absent?
>
> If not, no need to provide code: can write the loop myself.


Look up std::map::lower_bound

HTH,
- Anand


--

Bart van Ingen Schenau

unread,
Oct 31, 2009, 7:16:38 PM10/31/09
to
DerekBaker wrote:

> For a map of int, is it possible to use an STL algorithm to find a key
> that is less than or equal to
> a value, that itself may be absent?
>
> If not, no need to provide code: can write the loop myself.

Take a look at std::map<>::lower_bound and std::map<>::upper_bound.
They do not do exactly what you asked for, but they return an iterator
that is at most one position off.

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://c-faq.com/
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/

peter koch larsen

unread,
Oct 31, 2009, 7:14:38 PM10/31/09
to
On 30 Okt., 23:41, DerekBaker <m...@DontBother.com> wrote:
> For a map of int, is it possible to use an STL algorithm to find a key that is
> less than or equal to
> a value, that itself may be absent?
>
Yes. Read the manual. ;-) std::lower_bound and cousin will help you.

/Peter

--

0 new messages