rwallace
rwallace

Reputation: 33475

Why were the std::vector::erase parameters changed to const_iterator?

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?

Upvotes: 1

Views: 147

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275760

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.

Upvotes: 2

Related Questions