473,394 Members | 1,778 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,394 software developers and data experts.

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 14846
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 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. Though a hash table might have been a better choice,...
0
by: Erik Arner | last post by:
Hi, let's say I have a std::map<std::string,int> and I want to search the map for all keys that start with "foo". The regexp equivalent is to search for "foo*", or perhaps "^foo*". At present...
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<time_t, myclass> I would like to do calculations (min and max values) for a set of 'second' for a...
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 key_type& k) or calling (option #2)
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 t=thequeue.rend(); while (i!=thequeue.rend()) { if...
3
by: Henrik Goldman | last post by:
Hello, Assume we have a std::map<time_t, int>. Now I would like to find all values (second) which fits in (within the time range of first) between first of April 1st. and end of July. Notice...
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 depending on a DWORD value that is in range of two...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.