|
Prev: Is it allowed that std::map equal_range returns more than 1 elements?
Next: Wishlist: To override "C" pointers with C++ wrappers
From: qlin88 on 3 Apr 2008 06:07 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 help. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Carl Barron on 3 Apr 2008 10:35 In article <beab93ac-d4ff-441a-8ac3-06702c2ef8ab(a)d21g2000prf.googlegroups.com>, <qlin88(a)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! ]
From: Jiri Palecek on 3 Apr 2008 22:37 qlin88(a)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! ]
From: Andrew Koenig on 7 Apr 2008 07:35
<qlin88(a)gmail.com> wrote in message news:beab93ac-d4ff-441a-8ac3-06702c2ef8ab(a)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! ] |