std::pop_heap
STD:POP[医]堆
Defined in header | | |
---|---|---|
template< class RandomIt > void pop_heap( RandomIt first, RandomIt last | (1) | |
template< class RandomIt, class Compare > void pop_heap( RandomIt first, RandomIt last, Compare comp | (2) | |
交换头寸中的值first
以及该职位的价值last-1
并使子范围[first, last-1)
变成最大堆
这将从由范围定义的堆中移除第一个%28max%29元素。[first, last)
...
函数的第一个版本使用operator<为了比较元素,第二个函数使用给定的比较函数。comp...
参数
first, last | - | the range of elements defining the valid nonempty heap to modify |
---|---|---|
comp | - | comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b The signature does not need to have const &, but the function object must not modify the objects passed to it. The types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them. |
类型要求
---。
-取消引用的随机数的类型必须符合可移动分配和移动可建的要求。
返回值
%280%29
复杂性
顶多2×log%28N%29
比较N=
std::distance
(first, last)
...
注记
阿最大堆
是一系列元素[f,l)
具有以下属性:
- 带着N = l - f,为所有人0 < i < N,,,f[floor(i-12)]比不上f[i]...
- 可以使用
std::push_heap
()
- 第一个元素可以使用
std::pop_heap()
例
二次
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> v { 3, 1, 4, 1, 5, 9 };
std::make_heap(v.begin(), v.end()
std::cout << "v: ";
for (auto i : v) std::cout << i << ' ';
std::cout << '\n';
std::pop_heap(v.begin(), v.end() // moves the largest to the end
std::cout << "after pop_heap: ";
for (auto i : v) std::cout << i << ' ';
std::cout << '\n';
int largest = v.back(
v.pop_back( // actually removes the largest element
std::cout << "largest element: " << largest << '\n';
std::cout << "heap without largest: ";
for (auto i : v) std::cout << i << ' ';
std::cout << '\n';
}
二次
产出:
二次
v: 9 5 4 1 1 3
after pop_heap: 5 3 4 1 1 9
largest element: 9
heap without largest: 5 3 4 1 1
二次
另见
push_heap | adds an element to a max heap (function template) |
---|---|
make_heap | creates a max heap out of a range of elements (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。