STL vector with reverse and pop / push _back value

I am not very well versed in the cost of the algorithm, so here I ask.

Here is a vector initialized with 1000 elements:

vector<unsigned int> mFreeIndexes(1000);

I will constantly add pop_back / push_back elements to the vector, but never push_back more than 1000 (so never force the vector to redistribute).

In this case, the pop_back / push_back operations will be O (1) or O (n)?

+4
source share
1 answer

From the C ++ 23.3.7.5 standard:

void push_back (const T & x);

void push_back (T && x);

Notes: causes redistribution if the new size is larger than the old capacity (...)

, , , . , , push_back .

pop_back . pop_back. , , ( ), pop_back . :

pop_back() ? (++)

, , , , O (1).

+3

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


All Articles