468,309 Members | 1,074 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,309 developers. It's quick & easy.

STL map: reverse iterator for lower_bound?

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?

I tried to interate backwards with an iterator, but the begin()
element was not properly included.

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

Apr 3 '08 #1
4 12434
On 2008-04-03 23:07, ql****@gmail.com wrote:
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?

I tried to interate backwards with an iterator, but the begin()
element was not properly included.
You can use reverse_iterators and rend():

#include <iostream>
#include <map>

int main()
{
std::multimap<int, intm;
for (int i = 0; i < 10;++i)
{
m.insert(std::make_pair(i, i));
m.insert(std::make_pair(i, 2*i));
}

//for (
std::multimap<int, int>::iterator it = m.begin();
it != m.end();
++it)
// std::cout << it->first << "\t" << it->second << "\n";
std::multimap<int, int>::iterator upper = m.upper_bound(4);
std::multimap<int, int>::reverse_iterator r(upper);

for (;r != m.rend();++r)
std::cout << r->first << "\t" << r->second << "\n";
}
--
Erik Wikström
Apr 3 '08 #2
In article
<be**********************************@d21g2000prf. googlegroups.com>,
<ql****@gmail.comwrote:
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?
well n2521,pdf [mulitmap] states equal_range returns
pair<iterator,iterator>, [or possibly
pair<const_iterator,const_iterator>

where the range [pair::first,pair::second) is the range of equal keys.

if your implimentation of multimap returns only an iterator it is not
conforming....

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

Apr 4 '08 #3
ql****@gmail.com wrote:
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?
You can convert any (bidirectional) iterator to its reverse by something
like that:

typedef std::reverse_iterator<Iterrev_it;
std::for_each(rev_it(end), rev_it(begin), ...);

Regards
Jiri Palecek

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

Apr 4 '08 #4
<ql****@gmail.comwrote in message
news:be**********************************@d21g2000 prf.googlegroups.com...
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?
I tried to interate backwards with an iterator, but the begin()
element was not properly included.
Rule of thumb: decrement before you use the iterator; increment after you
use it. So:

it = x.begin();
while (it != x.end()) {
// do something with *it
++it;
};

And, going the other way:

it = x.end();
while (it != x.begin()) {
--it;
// do something with *it
}
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Apr 7 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

44 posts views Thread by jmoy | last post: by
reply views Thread by Erik Arner | last post: by
3 posts views Thread by Henrik Goldman | last post: by
13 posts views Thread by zaineb | last post: by
8 posts views Thread by olanglois | last post: by
3 posts views Thread by Diego Martins | last post: by
3 posts views Thread by Henrik Goldman | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.