A question about "insert" function in vector

106 Views Asked by At

https://en.cppreference.com/w/cpp/container/vector/insert

Cppreference shows: iterator insert( const_iterator pos, const T& value ); and four other overloads.

But why the parameter is const_iterator but not iterator?

1

There are 1 best solutions below

1
Artyer On BEST ANSWER

Whether or not the iterator is const doesn't matter, since the container is the thing being modified (and insert is not a const-qualified member function), not the passed in iterator.

And this just makes it easier to use. A non-const iterator is convertible to a const_iterator (but not the other way around) so you can still easily use an iterator.

A somewhat relevant paper: https://wg21.link/N2350