c++ - Does deque provide O(1) complexity when inserting on top -
i going on this post , states deque provides efficent insetion @ top , bottom.however post here states time complexity of deque other o(n).i think if deque has efficent top , bottom insertion have o(1) while vector should have o(1) @ bottom insertion only. appreciate if clarify this
the cppreference entry std::deque gives following complexity:
the complexity (efficiency) of common operations on deques follows:
- random access - constant o(1)
- insertion or removal of elements @ end or beginning - amortized constant o(1)
- insertion or removal of elements - linear o(n)
which consistent draft c++ standard section 23.3.3.1
class template deque overview says (emphasis mine):
a deque sequence container that, vector (23.3.6), supports random access iterators. in addition, supports constant time insert , erase operations @ beginning or end; insert , erase in middle take linear time. is, a deque optimized pushing , popping elements @ beginning , end. vectors, storage management handled automatically.
for std::vector cppreference says:
the complexity (efficiency) of common operations on vectors follows:
- random access - constant o(1)
- insertion or removal of elements @ end - amortized constant o(1)
- insertion or removal of elements - linear in distance end of vector o(n)
which consistent draft standard section 23.3.6.1
class template vector overview:
a vector sequence container supports random access iterators. in addition, it supports (amortized) constant time insert , erase operations @ end; insert , erase in middle take linear time. storage management handled automatically, though hints can given improve efficiency.[...]
Comments
Post a Comment