1

According to e.g. https://en.cppreference.com/w/cpp/container/vector/erase the parameters to std::vector::erase were in C++11 changed from iterator to const_iterator.

This is surprising; logically, the container does have to change the data pointed to by those iterators, and indeed when I implemented my own vector class, the compiler complained that I was calling memmove with a const pointer; I fixed it by changing the parameters back to iterator.

What's the logic behind making them const_iterator?

rwallace
  • 31,405
  • 40
  • 123
  • 242
  • 1
    The erase function is still nonconst, but you can call it now with a const iterator. erase doesn't actually use the content of the iterator more than to calculate an offset and then a pointer really. – Andrei Damian Nov 04 '18 at 22:46
  • 1
    erase doesn't modify the thing being erased –  Nov 04 '18 at 22:48
  • 2
    Check this answer: https://stackoverflow.com/a/4888697/3585576 – ihavenoidea Nov 04 '18 at 22:49

1 Answers1

2

The iterator just says where. The vector is non-const, and is from which it is erased.

This lets you find the location to be erased in a cost manner, and only when you actually erase it do you need a non const container.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524