stl list - complexity

后端 未结 5 2280
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 22:19

Are all the inserts (anywhere) for the list constant?

What about access?

Front, back - constant time?

and in the middle of the list - linear time?

5条回答
  •  失恋的感觉
    2020-12-19 23:25

    http://www.sgi.com/tech/stl/List.html

    A list is a doubly linked list. That is, it is a Sequence that supports both forward and backward traversal, and (amortized) constant time insertion and removal of elements at the beginning or the end, or in the middle. Lists have the important property that insertion and splicing do not invalidate iterators to list elements, and that even removal invalidates only the iterators that point to the elements that are removed

    With regards to access, if you're going to search for an element somewhere in the middle, it'll take linear time. But once you've got an iterator, it'll be (of course) constant time access, and it won't be invalidated by other insertions or removals.

提交回复
热议问题