stl::map iterator

H

HungryGoat

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!
 
Ö

Öö Tiib

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 said:
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.
 
G

guinness.tony

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

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top