473,804 Members | 3,515 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Thanks for your help.
--
[ 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 14981
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
<be************ *************** *******@d21g200 0prf.googlegrou ps.com>,
<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
news:be******** *************** ***********@d21 g2000prf.google groups.com...
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.

Similar topics

44
8811
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, I chose to use std::map. However, the program runs at less than half the speed of an equivalent program that uses ordinary handcoded binary trees. The result is not changed very much if I replace std::string as the key with a simple string class...
0
1887
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 I do this quick'n'dirty by appending a tilde (~) to the query term, since I know it's last in the ascii table and my keys don't include any special characters. So to find everything that starts with "foo" I search the map from...
3
3543
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 specific time range. Given the fact that 'first' can hold any time I would like to do the calculation based on values which falls within a range of e.g. yesterday
13
20165
by: zaineb | last post by:
Hi, This is a follow-up of sort of this thread: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/de12af6539e0d645/662cab752779f1fa?lnk=st&q=c%2B%2B+vector+vs+map&rnum=1&hl=en#662cab752779f1fa I've been chewing on this problem for a while. When I came across the above thread, I figured that maybe other members have a better idea on how to approach this. I'm currently trying to decide between which one of maps or...
8
6172
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
1252
by: Diego Martins | last post by:
Andrew Koenig wrote:
3
7472
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 (i->second->isComplete()) { t=i; i--; if (clean)
3
3316
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 that we did not yet reach end of July yet. Using lower_bound I can easily find the first data value within this range. However with upper_bound I get an invalid iterator when trying to get a future data. Ideally I would like the last record which...
2
5091
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 DWORD values (see below for more). So what I trying to achieve is creating a look up table of a IP adress with a start adress (a DWORD value) and a end adress (a DWORD value) which I would like to look up to the get the Country name
0
9576
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10311
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9138
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2988
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.