Map change access via iterator

Hi All,

A quick question. I'm trying to find how to alter a map container via a iterator.
This was posed as a test question in a book. To pass an output iterator to a function to save passing the whole container.

I'm really pulling my hair out on this one.

Cheers in Advance.

#include <iostream>
#include <map>
using namespace std;

int main ()
{
map<char,int> mymap;
map<char,int>::iterator it;

it = mymap.begin();
*it = (pair<char,int>('b',150));// error on this line:- error C2166: l-value specifies const object

}
For a std::map< K, V >, the type of the expression
*( std::map< K, V >::iterator ) is std::pair< const K, V >
because you can't modify the key without the container's
knowledge; this would break the ordering.

But you can do:
it->second = 150;

Thanks for the swift reply.

Having the key non changeable makes total sense. Is there any way of 'insert'ing a member via an iterator. I suppose if you pass the container by reference then call insert on this. It will save passing the whole container or creating the whole container and returning it.

Anyway Thanks again.

:)
Topic archived. No new replies allowed.