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 > about end() usage
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
about end() usage

Réponse
 
LinkBack Outils de la discussion
Vieux 01/07/2008, 10h53   #1
zhangyefei.yefei@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut about end() usage

can someone tell me ,is the following about end() right ? the
printed result seems ok,but i am not sure if i can use end() such
way.
thanks.

#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

mymap['a']=20;
mymap['b']=40;
mymap['c']=60;
mymap['d']=80;
mymap['e']=100;

it=mymap.lower_bound ('b'); // it points to b

for ( ; it != mymap.end(); it--)
cout << (*it).first << " => " << (*it).second << endl;

return 0;
}

the output is:
b => 40
a => 20
  Réponse avec citation
Vieux 01/07/2008, 11h07   #2
Kai-Uwe Bux
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about end() usage

zhangyefei.yefei@gmail.com wrote:

> can someone tell me ,is the following about end() right ? the
> printed result seems ok,but i am not sure if i can use end() such
> way.


No, you cannot.

> thanks.
>
> #include <iostream>
> #include <map>
> using namespace std;
>
> int main ()
> {
> map<char,int> mymap;
> map<char,int>::iterator it;
>
> mymap['a']=20;
> mymap['b']=40;
> mymap['c']=60;
> mymap['d']=80;
> mymap['e']=100;
>
> it=mymap.lower_bound ('b'); // it points to b
>
> for ( ; it != mymap.end(); it--)
> cout << (*it).first << " => " << (*it).second << endl;
>
> return 0;
> }
>
> the output is:
> b => 40
> a => 20


It appears that you hit upon an interesting implementation detail of your
STL. Your code has undefined behavior. The function end() yields a
past-last iterator not a pre-first iterator.

If you want to traverse backwards, you could use reverse iterators or
rewrite your loop.


Best

Kai-Uwe Bux
  Réponse avec citation
Vieux 01/07/2008, 15h43   #3
Andrew Koenig
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about end() usage

<zhangyefei.yefei@gmail.com> wrote in message
news:deae0df6-bed0-4f70-9f8d-56c5ab2a16cd@u36g2000prf.googlegroups.com...

> can someone tell me ,is the following about end() right ? the
> printed result seems ok,but i am not sure if i can use end() such
> way.


> it=mymap.lower_bound ('b'); // it points to b
>
> for ( ; it != mymap.end(); it--)
> cout << (*it).first << " => " << (*it).second << endl;


This isn't legitimate; if it happens to do what you want, it's an accident.

The following will work, and will produce the same result if 'b' is a key:

it = mymap.upper_bound('b');
while (it != mymap.begin()) {
--it;
cout << (*it).first << " => " << (*it).second << endl;
}

If 'b' is not a key, then this version will differ from yours in that the
first output will be the last key less than 'b' rather than the first key
greater than 'b'.


  Réponse avec citation
Vieux 02/07/2008, 02h22   #4
zhangyefei.yefei@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about end() usage

On Jul 1, 10:43pm, "Andrew Koenig" <a...@acm.org> wrote:
> <zhangyefei.ye...@gmail.com> wrote in message
>
> news:deae0df6-bed0-4f70-9f8d-56c5ab2a16cd@u36g2000prf.googlegroups.com...
>
> > can someone tell me ,is the following about end() right ? the
> > printed result seems ok,but i am not sure if i can use end() such
> > way.
> > it=mymap.lower_bound ('b'); // it points to b

>
> > for ( ; it != mymap.end(); it--)
> > cout << (*it).first << " => " << (*it).second << endl;

>
> This isn't legitimate; if it happens to do what you want, it's an accident..
>
> The following will work, and will produce the same result if 'b' is a key:
>
> it = mymap.upper_bound('b');
> while (it != mymap.begin()) {
> --it;
> cout << (*it).first << " => " << (*it).second << endl;
> }
>
> If 'b' is not a key, then this version will differ from yours in that the
> first output will be the last key less than 'b' rather than the first key
> greater than 'b'.


the question is that if "it" happens to be the first element (that
is mymap.begin() ),then we can not access the first
element,nothing is outputed.
  Réponse avec citation
Vieux 02/07/2008, 15h13   #5
Jerry Coffin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about end() usage

In article <94394196-cb35-4d7f-9754-
ba7280cab66d@h1g2000prh.googlegroups.com>, zhangyefei.yefei@gmail.com
says...
> On Jul 1, 10:43Âpm, "Andrew Koenig" <a...@acm.org> wrote:
> > <zhangyefei.ye...@gmail.com> wrote in message
> >
> > news:deae0df6-bed0-4f70-9f8d-56c5ab2a16cd@u36g2000prf.googlegroups.com....
> >
> > > can someone tell me ,is the following about end() Â right ? Âthe
> > > printed result Âseems ok,but i am not sure if i can use end() such
> > > way.
> > > Âit=mymap.lower_bound ('b'); Â// it points to b

> >
> > > Âfor ( Â; it != mymap.end(); it--)
> > > Â Âcout << (*it).first << " => " << (*it).second << endl;

> >
> > This isn't legitimate; if it happens to do what you want, it's an accident.
> >
> > The following will work, and will produce the same result if 'b' is a key:
> >
> > Â Â it = mymap.upper_bound('b');
> > Â Â while (it != mymap.begin()) {
> > Â Â Â Â --it;
> > Â Â Â Â cout << (*it).first << " => " << (*it).second << endl;
> > Â Â }
> >
> > If 'b' is not a key, then this version will differ from yours in that the
> > first output will be the last key less than 'b' rather than the first key
> > greater than 'b'.

>
> the question is that if "it" happens to be the first element (that
> is mymap.begin() ),then we can not access the first
> element,nothing is outputed.


#include <map>
#include <iostream>
#include <algorithm>

typedef std::pair<char, int> mytype;

// technically not allowed...
namespace std {
ostream &operator<<(ostream &os, mytype const &v) {
return os << v.first << " => " << v.second;
}
}

int main() {

std::map<char, int> mymap;

for (int i=0; i<10; i++)
mymap['b'+i] = i+1;

std::cout << "searched key is first item in map:\n";
std::reverse_copy(mymap.begin(), mymap.upper_bound('b'),
std:stream_iterator<mytype>(std::cout, "\n"));

mymap['a'] = 0;

std::cout << "\nSearched item is not first item in map:\n";
std::reverse_copy(mymap.begin(), mymap.upper_bound('b'),
std:stream_iterator<mytype>(std::cout, "\n"));
return 0;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
  Réponse avec citation
Vieux 03/07/2008, 13h43   #6
Andrew Koenig
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: about end() usage

<zhangyefei.yefei@gmail.com> wrote in message
news:94394196-cb35-4d7f-9754-ba7280cab66d@h1g2000prh.googlegroups.com...
On Jul 1, 10:43 pm, "Andrew Koenig" <a...@acm.org> wrote:

>> The following will work, and will produce the same result if 'b' is a
>> key:
>>
>> it = mymap.upper_bound('b');
>> while (it != mymap.begin()) {
>> --it;
>> cout << (*it).first << " => " << (*it).second << endl;
>> }
>>
>> If 'b' is not a key, then this version will differ from yours in that the
>> first output will be the last key less than 'b' rather than the first key
>> greater than 'b'.


> the question is that if "it" happens to be the first element (that
> is mymap.begin() ),then we can not access the first
> element,nothing is outputed.


Indeed. But this will never happen if 'b' is a key in the map. Please note
the change from lower_bound in the original program to upper_bound in my
version. Note that upper_bound returns an iterator that refers to the first
element *after* the given key.


  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 18h29.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15928 seconds with 14 queries