c++ - STL vector with reverse and pop/push_back cost -
i not come algorithm costs, here asking.
here vector initialized 1000 elements:
vector<unsigned int> mfreeindexes(1000);
i continuously pop_back/push_back elements vector, never push_back on 1000 (so never force vector reallocate).
in case pop_back/push_back operations o(1) or o(n)?
from c++ standard 23.3.7.5:
void push_back(const t& x);
void push_back(t&& x);
remarks: causes reallocation if new size greater old capacity (...)
note doesn't can't reallocate in other scenario unusual implementation of standard. think can safely assume push_back
won't reallocate when there's still capacity.
the thing pop_back
bit more complicated. standard not reallocation in pop_back
context. seems common implementation (with no know exception) pop_back
not reallocate. there guarantees though, see this:
can pop_back() ever reduce capacity of vector? (c++)
anyway long don't go on predefined size safe assume no reallocation happens , complexity indeed o(1).
Comments
Post a Comment