如何将3个元素放在c++的Priority_queue中

发布于 2025-02-04 23:28:37 字数 604 浏览 3 评论 0原文

我一直在研究Leetcode中的C ++中的Priority_queue,我从解决方案中找到了此代码 我知道这是最小堆,但不明白这将如何存储3个元素给Minheap。

  1. IS vector< int>获取matrix [r] [0]vector< vector< vector< int>>>> get> r r r 和更大<>获取0 ????
  2. 为什么我们需要放置Priority_queue< int,vector< int> Minheap a vector< int要使最小堆?

enter image description here

I been studying priority_queue in c++ from Leetcode and I found this code from solution
I understand that this is minimum heap but don't understand how is this storing 3 elements to minHeap.

  1. is vector<int> gets matrix[r][0], vector<vector<int>> gets r and greater<> gets 0????
  2. Why do we need to put priority_queue<int,vector<int>,greater<int>> minHeap a vector<int> to make Minimum heap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼泪都笑了 2025-02-11 23:28:37

首先,让我们看一下minheap类中的模板参数的含义。

来自 cppreference

 模板&lt;
    T级,
    类容器= std :: vector&lt; t&gt; ,,
    类比较= std :: Less&lt; typename容器:: value_type&gt;
class Priority_queue;
 

模板参数

t - 存储元素的类型。 ...

容器 - 用于存储元素的基础容器的类型。 ...

比较 - 一个比较类型提供严格的弱点。

因此,对于minheap,它包含vector&lt; int&gt;对象。它使用vector&lt; vector&lt; int&gt;&gt;作为基础存储,以包含这些vector&lt; int&gt;对象。 &lt;&gt; 比较两个vector&lt; int对象来决定它们的顺序。


它使用更大的 >表示? 比较参数用于确定哪个元素在顶部。默认情况下,Priority_queue使用LIST&lt;&gt;,这意味着较大的元素在顶部。通过使用更大的&lt;&gt;,它会翻转比较的方向,因此较小的元素在顶部。这就是使它成为“最小”堆的原因。


现在,查看 push

  void push(const value_type&amp; value);
void push(value_type&amp;&amp; value); (由于C ++ 11)
 

push仅接受一个参数,该参数是类型value_type的参数,在这种情况下为vector&lt; int&gt;

因此,该行

minHeap.push({matrix[r][0], r, 0});

实际上是将单个向量&lt;添加到minheap中。该vector&lt; int&gt;中包含的值是矩阵[r] [0]r0

First, let's look at the meaning of the template arguments in the class of minHeap.

From cppreference:

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
class priority_queue;

Template parameters

T - The type of the stored elements. ...

Container - The type of the underlying container to use to store the elements. ...

Compare - A Compare type providing a strict weak ordering.

So, for minHeap, it contains vector<int> objects. It uses a vector<vector<int>> as the underlying storage to contain these vector<int> objects. And it uses greater<> to compare two vector<int> objects to decide what order they go in.


What does the greater<> signify? The Compare argument is used to decide which element goes on top. By default, priority_queue uses less<>, which means that larger elements go on top. By using greater<> instead, it flips the direction of the comparison, so smaller elements go on top. This is what makes it a "min" heap.


Now, looking at the call to push:

void push( const value_type& value );
void push( value_type&& value ); (since C++11)

push is only accepting a single argument, and the argument is of type value_type, which in this case is vector<int>.

So the line

minHeap.push({matrix[r][0], r, 0});

Is actually adding a single vector<int> to minHeap. The values contained in that vector<int> are matrix[r][0], r, and 0.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文