[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

std::map operator[] invalidating iterators?

alan

11/24/2008 2:26:00 PM

First off, here's what I want to do: I want to iterate over the key-
value pairs of an std::map, and change only its value.

AFAICT, however, std::map::begin() returns a const_iterator, so I
can't actually change the value using just the iterator. What I
thought I'd do would be, I'd iterate using the iterator, get the key,
then use std::map::operator[] to change its values, like so:

typedef std::map<Generic*, Generic*> trmap;
for(trmap::iterator it = m.begin(); it != m.end(); ++i) {
Generic* K = it->first;
m[K] = transforming_function(m[K]);
}

Would this be valid and standards-compliant?
1 Answer

Maxim Yegorushkin

11/24/2008 3:08:00 PM

0

On Nov 24, 2:26 pm, amkg <almkg...@gmail.com> wrote:
> First off, here's what I want to do: I want to iterate over the key-
> value pairs of an std::map, and change only its value.
>
> AFAICT, however, std::map::begin() returns a const_iterator

const_iterator is returned by the const version of std::map::begin().
Non-const version of std::map::begin() returns a (non-const) iterator,
using which you can change the values (but not the keys).

--
Max