std::pop_heap_C++中文网
| 定义于头文件 |
||
| (1) | ||
| template< class RandomIt > |
(C++20 前) | |
| template< class RandomIt > |
(C++20 起) | |
| (2) | ||
| template< class RandomIt, class Compare > |
(C++20 前) | |
| template< class RandomIt, class Compare > |
(C++20 起) | |
交换在位置 first 的值和在位置 last-1 的值,并令子范围 [first, last-1) 变为堆。这拥有从范围 [first, last) 所定义的堆移除首个元素的效果。
函数的首个版本使用 operator< 比较元素,这使堆成为最大堆。第二版本使用给定的比较函数 comp 。
参数
| first, last | - | 定义要修改的合法非空堆的元素范围 |
| comp | - | 比较函数对象(即满足比较 (Compare) 要求的对象),若首个参数小于第二个,则返回 true 。
比较函数的签名应等价于如下: bool cmp(const Type1 &a, const Type2 &b); 虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 |
| 类型要求 | ||
-RandomIt 必须满足值可交换 (ValueSwappable) 和 遗留随机访问迭代器 (LegacyRandomAccessIterator) 的要求。
| ||
-解引用 RandomIt 结果的类型必须满足可移动赋值 (MoveAssignable) 和可移动构造 (MoveConstructible) 的要求。
| ||
返回值
(无)
复杂度
至多 2×log(N) 次比较,其中 N=std::distance(first, last).
注意
最大堆是拥有下列属性的元素范围 [f,l) :
- 设
N = l - f,对于所有0 < i < N,f[floor(不小于
)]i-1 2 f[i]。 - 可用 std::push_heap() 添加新元素
- 可用 std::pop_heap() 移除首元素
- 设
示例
输出:
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