PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > STL map: reverse iterator for lower_bound?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
STL map: reverse iterator for lower_bound?

Réponse
 
LinkBack Outils de la discussion
Vieux 03/04/2008, 22h07   #1
qlin88@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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 .


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

  Réponse avec citation
Vieux 03/04/2008, 22h25   #2
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL map: reverse iterator for lower_bound?

On 2008-04-03 23:07, qlin88@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, int> m;
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
  Réponse avec citation
Vieux 04/04/2008, 02h35   #3
Carl Barron
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL map: reverse iterator for lower_bound?

In article
<beab93ac-d4ff-441a-8ac3-06702c2ef8ab@d21g2000prf.googlegroups.com>,
<qlin88@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?
>

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! ]

  Réponse avec citation
Vieux 04/04/2008, 14h37   #4
Jiri Palecek
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL map: reverse iterator for lower_bound?

qlin88@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<Iter> rev_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! ]

  Réponse avec citation
Vieux 07/04/2008, 23h35   #5
Andrew Koenig
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: STL map: reverse iterator for lower_bound?

<qlin88@gmail.com> wrote in message
news:beab93ac-d4ff-441a-8ac3-06702c2ef8ab@d21g2000prf.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! ]

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h12.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16816 seconds with 13 queries