Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > stl::map iterator

Reply
Thread Tools

stl::map iterator

 
 
HungryGoat
Guest
Posts: n/a
 
      07-01-2013
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!
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      07-01-2013
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.


 
Reply With Quote
 
 
 
 
guinness.tony@gmail.com
Guest
Posts: n/a
 
      07-01-2013
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).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL map: reverse iterator for lower_bound? qlin88@gmail.com C++ 3 04-07-2008 10:35 PM
STL Map iterator compilation error Rakesh Kumar C++ 2 11-29-2007 01:14 AM
STL / iterator / map - I dont get it cppquest C++ 5 02-22-2007 09:41 PM
How to delete stl<map> element from iterator? Steve Edwards C++ 11 02-23-2006 04:23 PM
std::map::iterator with STL algorithms rg C++ 1 07-22-2004 10:31 AM



Advertisments