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

I know that it is std::list::operator[]not implemented, since it has poor performance. But what about std::vector::insert, it is as inefficient as it is std::list::operator[]. What is the explanation?

+4
source share
4 answers

std::vector::insertis implemented because it std::vectormust satisfy the requirements of the SequenceContainer concept , and is operator[]not required by any concepts (which I know), it is possible that it will be added to the concept ContiguousContainerin C ++ 17. Thus, it is operator[]added to containers that can be used as arrays, while it insertis required according to the specification of the interface, so containers that correspond to a specific concept can be used in general algorithms.

+9
source

, , . , , , . , , , , , .

operator[], , , ( , vector). insert . , , vector, , . , . insert, ( , ). .

, operator[] , .

+2

std::list::operator[] O (N) , list. operator[], . ++ a [], O (1) (, , O (Log N)). [] list .

std::vector::insert O (N), : vector . , .

+1

[] . ( ) . , .

auto a = Container [10]; // Ideally I can assume this is quick

std::list <>::operator [] std::next <std::list<>::iterator>. cpp-reference.

auto a = *std::next (Container.begin (), 10); // This may take a little while

. , , , .

0

Source: https://habr.com/ru/post/1650323/


All Articles