Narek Narek - 4 months ago 23
C++ Question

std::vector::insert vs std::list::operator[]

I know that

std::list::operator[]
is not implemented as it has bad performance. But what about
std::vector::insert
it is as much inefficient as much
std::list::operator[]
is. What is the explanation behind?

Answer

std::vector::insert is implemented because std::vector has to meet requirements of SequenceContainer concept, while operator[] is not required by any concepts (that I know of), possible that will be added in ContiguousContainer concept in c++17. So operator[] added to containers that can be used like arrays, while insert is required by interface specification, so containers that meet certain concept can be used in generic algorithms.