std::queue >::size() is slow in O(n)?

后端 未结 3 2083
你的背包
你的背包 2021-01-12 12:08

I experienced unexpected performance behavior of my code which uses a queue. I realized that performance degraded when more elements were in the queue. It turned out that us

3条回答
  •  隐瞒了意图╮
    2021-01-12 12:44

    queue is a container adaptor, so you have to understand that the complexity descriptions may refer only to the work the adaptor does itself (which is indeed constant, namely just passing the call through to the underlying container).

    For example, see this reference: size() calls the underlying container's size() function. For a list, this has complexity O(n) in C++98/03, and O(1) in C++11.

提交回复
热议问题