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