小编Vah*_*idi的帖子

使用`std :: greater`通过`priority_queue`创建最小堆的原因

我想知道为什么使用the创建一个最小堆priority_queue,std::greater应该使用?

std::priority_queue<T, std::vector<T>, std::greater<T> > min_heap;
Run Code Online (Sandbox Code Playgroud)

对我来说,由于最小的值总是位于堆的顶部,所以应用的类应该是 std::less

更新: 另一方面,由于priority_queue(max heap)的默认行为是在顶部保持最大值,因此我认为std::greater应该用于最大堆创建而不是用于创建最小堆

c++ heap priority-queue min-heap c++11

16
推荐指数
2
解决办法
2846
查看次数

为什么std :: shared_ptr <T []>没有专门化?

我想知道为什么动态数组直接支持std::unique_ptr<>但不支持std::shared_ptr<>:

unique_ptr<int[]> ptr1(new int[n]); /// OK!
shared_ptr<int[]> ptr2(new int[n]); /// Incorrect: will not call delete[]
Run Code Online (Sandbox Code Playgroud)

更新:我发现第二行可以改写为:

 shared_ptr<int> ptr2(new int[n], default_delete<int[]>());
Run Code Online (Sandbox Code Playgroud)

现在我想知道幕后发生了什么,这使得std::shared_ptr采用第二种方法而不是类似的方式std::unique_ptr

c++ c++11

7
推荐指数
1
解决办法
262
查看次数

C++ 14中的尾随返回类型

随着autoC++ 14 中返回类型的引入,是否存在需要尾随返回类型或者在C++ 14和17中完全过时的实际情况?

c++ auto c++14

5
推荐指数
2
解决办法
917
查看次数

标签 统计

c++ ×3

c++11 ×2

auto ×1

c++14 ×1

heap ×1

min-heap ×1

priority-queue ×1