简体   繁体   English

为什么在定义结构的优先级队列时使用向量?

[英]Why is a vector used while defining priority queue of structures?

I know that we can define a priority queue of structures in the following way -我知道我们可以通过以下方式定义结构的优先级队列 -

std::priority_queue<somestructure, vector<somestructure>, compare> pq;

Where compare is the structure which contains the compare function.其中 compare 是包含比较函数的结构。 I want to ask why we need to use vector as the second argument in this declaration.我想问一下为什么我们需要在这个声明中使用 vector 作为第二个参数。 How is a vector related to a priority queue while defining the above priority queue?在定义上述优先级队列时,向量如何与优先级队列相关?

std::priority_queue is what we call a container adapter . std::priority_queue就是我们所说的容器适配器 As you know in C++ we got containers like std::vector , std:array or std::deque .如您所知,在 C++ 中,我们有像std::vectorstd:arraystd::deque这样的容器。 All those are there to directly save stuff of type T into them, with different pros and cons.所有这些都可以直接将T类型的东西保存到它们中,各有利弊。

std::stack for example is a container adapter, which can be used on top of std:deque .例如std::stack是一个容器适配器,可以在std:deque之上使用。 The only thing this adapter does is to take away the functionality of std::deque to insert at the end, or take from the end.这个适配器所做的唯一一件事就是std::deque在末尾插入或从末尾取出的功能。 By this the user is forced to use the std::deque only like a stack.通过这种方式,用户只能像堆栈一样使用std::deque

Something similiar is true for std::priority_queue , it forces you to only insert into the underlying container, a std::vector for example, in an order. std::priority_queue也有类似的情况,它强制您只能按std::priority_queue插入底层容器,例如std::vector By this you get some nice properties of how to find elements in this container.通过这种方式,您可以获得有关如何在此容器中查找元素的一些不错的属性。 In this special case, by taking more effort into how to insert new elements in the underlying container, and taking away the freedom to randomly insert an element anywhere you like.在这种特殊情况下,通过更多地研究如何在底层容器中插入新元素,并剥夺在您喜欢的任何地方随机插入元素的自由。 So you got O(log(n)) complexity for the insert instead of O(1) (instead of inserting at the end for example).因此,插入的复杂度为 O(log(n)),而不是 O(1)(例如,而不是在末尾插入)。 But by that you have a complexity of only O(1) to find the largest element, instead of O(n).但是这样一来,找到最大元素的复杂度仅为 O(1),而不是 O(n)。

There is nothing special about std::vector in this case, you could use any container that satisfy the needs of this container adaptor , you could also use std::deque or your own container or to quote :在这种情况下std::vector没有什么特别之处,您可以使用满足此容器适配器需求的任何容器,您也可以使用std::deque或您自己的容器或引用

The type of the underlying container to use to store the elements.用于存储元素的底层容器的类型。 The container must satisfy the requirements of SequenceContainer, and its iterators must satisfy the requirements of LegacyRandomAccessIterator.容器必须满足 SequenceContainer 的要求,其迭代器必须满足 LegacyRandomAccessIterator 的要求。 Additionally, it must provide the following functions with the usual semantics: front() push_back() pop_back() The standard containers std::vector and std::deque satisfy these requirements.此外,它必须提供以下具有通常语义的函数: front() push_back() pop_back() 标准容器 std::vector 和 std::deque 满足这些要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM