472,336 Members | 1,213 Online

# 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.

--
[ 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 14205
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
<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
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 thread has been closed and replies have been disabled. Please start a new discussion.

### Similar topics

 44 by: jmoy | last post by: I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file.... 0 by: Erik Arner | last post by: Hi, let's say I have a std::map and I want to search the map for all keys that start with "foo". The regexp equivalent is to... 3 by: Henrik Goldman | last post by: I have a std::map which consist of time_t as first and an associated class as second: map I would like to do calculations... 13 by: zaineb | last post by: Hi, This is a follow-up of sort of this thread:... 8 by: olanglois | last post by: Hi, I was asking myself to following question. What is better to erase an element from a STL map: calling (option #1) size_type erase(const... 3 by: Diego Martins | last post by: Andrew Koenig wrote: 3 by: noone | last post by: string operator()(const bool clean=true) { string rv; MPEGQUEUE::reverse_iterator i=thequeue.rbegin(); MPEGQUEUE::reverse_iterator... 3 by: Henrik Goldman | last post by: Hello, Assume we have a std::map. Now I would like to find all values (second) which fits in (within the time range of first)... 2 by: kl | last post by: Hi, I'm trying to learn some STL using map or hash_map would maybe even better to use but I don't really know how to find a specific struct... 0 by: concettolabs | last post by: In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool... 0 by: teenabhardwaj | last post by: How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of... 0 by: CD Tom | last post by: This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right... 0 by: CD Tom | last post by: This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins... 0 by: Naresh1 | last post by: What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge... 0 by: jalbright99669 | last post by: Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made... 0 by: antdb | last post by: Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was... 2 by: Matthew3360 | last post by: Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it... 0 by: AndyPSV | last post by: HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...