stl list - complexity

后端 未结 5 2281
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  梦毁少年i
    2020-12-19 23:03

    Note that, mainly due to better locality of data, in practice std::vector is often faster than std::list, even where in theory it should be the other way around. So the default sequential container should be std::vector.

    If you doubt, first measure whether that container is critical at all (no use in increasing the speed of a piece of code even ten times, if that piece only uses 2% of the overall time), then compare measurements with std::list and std::deque and make your pick.

提交回复
热议问题