STL map: reverse iterator for lower_bound?

Discussion in 'C++' started by qlin88@gmail.com, Apr 3, 2008.

  1. Guest

    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! ]
     
    , Apr 3, 2008
    #1
    1. Advertising

  2. On 2008-04-03 23:07, 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
     
    Erik Wikström, Apr 3, 2008
    #2
    1. Advertising

  3. Jiri Palecek Guest

    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! ]
     
    Jiri Palecek, Apr 4, 2008
    #3
  4. <> wrote in message
    news:...

    > 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! ]
     
    Andrew Koenig, Apr 7, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rg
    Replies:
    1
    Views:
    2,152
    tom_usenet
    Jul 22, 2004
  2. Fred Zwarts
    Replies:
    5
    Views:
    1,547
    Andrew Koenig
    Apr 23, 2006
  3. Diego Martins

    stl map, lower_bound and upper_bound

    Diego Martins, Aug 14, 2006, in forum: C++
    Replies:
    3
    Views:
    1,181
    Diego Martins
    Aug 15, 2006
  4. mattg

    STL set lower_bound

    mattg, Mar 29, 2009, in forum: C++
    Replies:
    9
    Views:
    1,240
    Dennis
    Mar 31, 2009
  5. Jim Anderson

    problem with iterator (map iterator)

    Jim Anderson, Jan 10, 2014, in forum: C++
    Replies:
    3
    Views:
    128
    Luca Risolia
    Jan 13, 2014
Loading...

Share This Page