|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
<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'. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
<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. |
|
![]() |
| Outils de la discussion | |
|
|