1

I am just curious to know if I can change/update the map's value using const_iterator.

Below is the code snippet:

int main()
{
    map <int, int> m;
    m.insert(make_pair(1, 10));

    map <int, int>::const_iterator itr = m.begin(); //The iterator is const_iterator
    itr->second = 30;

    cout << itr->second; //The value to be printed is 30, and not 10.
    return 0;
}

Thank you in advance for sharing your ideas.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
pkthapa
  • 1,029
  • 1
  • 17
  • 27
  • 5
    Can you elaborate on why you think that should be possible? How do you understand the "const" in the name `const_iterator`? – Angew is no longer proud of SO Jul 24 '19 at 16:18
  • 1
    The code doesn't compile with the message "error: assignment of member 'std::pair::second' in read-only object". So clearly, you cannot do it with this code. Are you asking for a method to circumvent this behavior,.e.g. using a `const_cast` or something similar? – Gilles-Philippe Paillé Jul 24 '19 at 16:25

5 Answers5

7

The whole point of const_iterator is that it cannot be used to modify the container. So no.

Jesper Juhl
  • 30,449
  • 3
  • 47
  • 70
3

It is not possible to change an element through a const iterator. That's the most important distinction between a const iterator and non-const iterator.

std::map::begin returns a non-const iterator (assuming the object operand is non-const), so there is no need to use a const iterator in the first place.

However, if for some reason (not demonstrated in the example) you can only have a const iterator, but have non-const access to the container, then you can get a non-const iterator to the element pointed by the const iterator. This can be achieved by using following, which looks like a trick:

map <int, int>::iterator mutable_itr = m.erase(itr, itr);

It doesn't erase anything, because [itr, itr) is an empty range.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 1
    First impression: euww, that's horrible! But I see the cleverness, and it's still safe, because a non-const `m` has no `erase()`. – Toby Speight Jul 26 '19 at 08:13
  • 1
    @TobySpeight That was my first impression as well. My second impression: Why on earth is there no standard function to do this without cleverness? – eerorika Jul 26 '19 at 11:48
2

Can I change/update the map's value using const_iterator?

No!

It's called const iterator for a reason. As mentioned here, A const_iterator is an iterator that points to const value (like a const T* pointer); dereferencing it returns a reference to a constant value (const T&) and prevents modification of the referenced value: it enforces const-correctness.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
1

The compiler should not allow modifying the contents of a map through a const_iterator. The line

itr->second = 30;

should be reported as an error. If your compiler allows that line, then it is not standards compliant. Perhaphs it allows that line to be compiled through flags that allow standards-incompatible behavior.

Using g++, I get the following error.

socc.cc: In function ‘int main()’:
socc.cc:12:19: error: assignment of member ‘std::pair<const int, int>::second’ in read-only object
     itr->second = 30;
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

const iterator is supposed to prevent the user from changing/modiyfing the value by any means.

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Oblivion
  • 7,176
  • 2
  • 14
  • 33