473,991 Members | 63,203 Online

STL map: reverse iterator for lower_bound?

Hi,

In STL multi-map, the lower_bound, upper_bound,equ al_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.

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

Apr 3 '08 #1
4 15041
On 2008-04-03 23:07, ql****@gmail.co m wrote:
Hi,

In STL multi-map, the lower_bound, upper_bound,equ al_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_iterato rs and rend():

#include <iostream>
#include <map>

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

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

for (;r != m.rend();++r)
std::cout << r->first << "\t" << r->second << "\n";
}
--
Erik WikstrÃ¶m
Apr 3 '08 #2
In article
<ql****@gmail.c omwrote:
Hi,

In STL multi-map, the lower_bound, upper_bound,equ al_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,i terator>, [or possibly
pair<const_iter ator,const_iter ator>

where the range [pair::first,pai r::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++.m oderated. First time posters: Do this! ]

Apr 4 '08 #3
ql****@gmail.co m wrote:
Hi,

In STL multi-map, the lower_bound, upper_bound,equ al_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_it erator<Iterrev_ it;
std::for_each(r ev_it(end), rev_it(begin), ...);

Regards
Jiri Palecek

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

Apr 4 '08 #4
<ql****@gmail.c omwrote in message
In STL multi-map, the lower_bound, upper_bound,equ al_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++.m oderated. First time posters: Do this! ]

Apr 7 '08 #5

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