首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::pop_heap

Defined in header <algorithm>

?

?

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()

二次

代码语言:javascript
复制
#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';
}

二次

产出:

二次

代码语言:javascript
复制
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)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com