stl::map iterator

Discussion in 'C++' started by HungryGoat, Jul 1, 2013.

  1. HungryGoat

    HungryGoat Guest

    Hi, I found this code in "The C++ Standard Library" book by Nikolai.

    template <class Cont>
    inline bool replace_key (Cont& c,
    const typename Cont::key_type& old_key,
    const typename Cont::key_type& new_key)
    {
    typename Cont::iterator pos;
    pos = c.find(old_key);
    if (pos != c.end()) {
    //insert new element with value of old element
    c.insert(typename Cont::value_type(new_key,
    pos->second));

    //remove old element
    c.erase(pos);
    return true;
    }
    else {
    //key not found
    return false;
    }
    }

    My question is, in the call to c.erase(pos), aren't there chances that pos is invalidated by the previous call to insert?

    I am under the impression that insert or delete invalidates the iterators.

    Cheers!
     
    HungryGoat, Jul 1, 2013
    #1
    1. Advertising

  2. HungryGoat

    Öö Tiib Guest

    On Monday, 1 July 2013 05:30:05 UTC+3, HungryGoat wrote:
    > Hi, I found this code in "The C++ Standard Library" book by Nikolai.
    >
    > template <class Cont>
    > inline bool replace_key (Cont& c,
    > const typename Cont::key_type& old_key,
    > const typename Cont::key_type& new_key)
    > {
    > typename Cont::iterator pos;
    > pos = c.find(old_key);
    > if (pos != c.end()) {
    > //insert new element with value of old element
    > c.insert(typename Cont::value_type(new_key,
    > pos->second));
    >
    > //remove old element
    > c.erase(pos);
    > return true;
    > }
    > else {
    > //key not found
    > return false;
    > }
    > }
    >
    > My question is, in the call to c.erase(pos), aren't there chances that
    > pos is invalidated by the previous call to insert?


    No. std::map<4>::insert may not invalidate any iterators.

    > I am under the impression that insert or delete invalidates the iterators.


    'delete' is keyword that yes invalidates everything that was pointed at.
    I assume you meant 'erase' however. std::map<4>::erase invalidates only
    iterators, pointers and references referring to elements removed. All others
    must keep validity.

    When you want to replace 'std::map' with 'boost::flat_map' then you must be
    careful since that thing is based on 'std::vector' and so 'insert'
    potentially invalidates all iterators.
     
    Öö Tiib, Jul 1, 2013
    #2
    1. Advertising

  3. HungryGoat

    Guest

    On Monday, 1 July 2013 03:30:05 UTC+1, HungryGoat wrote:
    > Hi, I found this code in "The C++ Standard Library" book by Nikolai.


    <snip>

    > My question is, in the call to c.erase(pos), aren't there chances that pos is invalidated by the previous call to insert?
    >
    > I am under the impression that insert or delete invalidates the iterators.


    Classes instantiated from the std::map template are Associative Containers.
    The Standard has this to say on the effects of insert/erase on their iterators:

    [associative.reqmts].para 9:

    "The insert members shall not affect the validity of iterators and references
    to the container, and the erase members shall invalidate only iterators and
    references to the erased elements."

    This is in stark contrast to the behaviour of std::vector (from which you
    probably formed your impression).
     
    , Jul 1, 2013
    #3
    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,165
    tom_usenet
    Jul 22, 2004
  2. Steve Edwards
    Replies:
    11
    Views:
    25,640
    Mike Wahler
    Feb 23, 2006
  3. cppquest
    Replies:
    5
    Views:
    384
    Dana Good
    Feb 22, 2007
  4. Rakesh Kumar

    STL Map iterator compilation error

    Rakesh Kumar, Nov 29, 2007, in forum: C++
    Replies:
    2
    Views:
    582
    Rakesh Kumar
    Nov 29, 2007
  5. Jim Anderson

    problem with iterator (map iterator)

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

Share This Page