本文介绍了当插入顶部时,deque提供O(1)复杂度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览讯息,并指出该deque在顶部和底部提供有效的insetion。然而,这这里指出,除了后面的deque的时间复杂度是O(n)。我会认为如果deque有效顶部和底部插入,它将有O(1),而一个向量应该有O(1)底部插入。如果有人可以澄清这一点,我将不胜感激。

解决方案

提供了以下复杂性:




  • 随机访问 - O(1)

  • 在结尾或开头插入或移除元素 - 摊销常数O(1)

  • 插入或移除元素 - 线性O(n)



,与部分 23.3.3.1 类模板deque概述强调我):

对于 cppreference说:




  • 随机访问 - 常数O li>
  • 在末尾插入或删除元素 - 摊销常量O(1)

  • 插入或删除元素 - 23.3.6.1 类模板向量概述


    I was going over this post and it states that deque provides efficent insetion at the top and bottom.However this post here states that time complexity of deque other than the back is O(n).I would think that if a deque has efficent top and bottom insertion it would have O(1) while a vector should have O(1) at bottom insertion only. I would appreciate it if someone could clarify this

    解决方案

    The cppreference entry for std::deque gives the following complexity:

    • Random access - constant O(1)
    • Insertion or removal of elements at the end or beginning - amortized constant O(1)
    • Insertion or removal of elements - linear O(n)

    which is consistent with the draft C++ standard section 23.3.3.1 Class template deque overview which says (emphasis mine):

    For std::vector cppreference says:

    • Random access - constant O(1)
    • Insertion or removal of elements at the end - amortized constant O(1)
    • Insertion or removal of elements - linear in distance to the end of the vector O(n)

    which is consistent with the draft standard section 23.3.6.1 Class template vector overview:

    这篇关于当插入顶部时,deque提供O(1)复杂度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 13:30