我想知道为什么使用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应该用于最大堆创建而不是用于创建最小堆
我想知道为什么动态数组直接支持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?
随着autoC++ 14 中返回类型的引入,是否存在需要尾随返回类型或者在C++ 14和17中完全过时的实际情况?