Stellarist
Stellarist

Reputation: 53

A question about "insert" function in vector

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?

Upvotes: 0

Views: 119

Answers (1)

Artyer
Artyer

Reputation: 40811

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

Upvotes: 2

Related Questions