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

std::push_heap

Defined in header <algorithm>

?

?

template< class RandomIt > void push_heap( RandomIt first, RandomIt last );

(1)

?

template< class RandomIt, class Compare > void push_heap( RandomIt first, RandomIt last, Compare comp );

(2)

?

将元素插入位置。last-1进入最大堆由范围定义[first, last-1)函数的第一个版本使用operator<为了比较元素,第二个函数使用给定的比较函数。comp...

参数

first, last

-

the range of elements defining the 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. ?

类型要求

---它必须符合RandomAccessIterator的要求。

-取消引用的随机数的类型必须符合可移动分配和移动可建的要求。

返回值

%280%29

复杂性

顶多日志%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';
 
    v.push_back(6);
 
    std::cout << "before push_heap: ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';
 
    std::push_heap(v.begin(), v.end());
 
    std::cout << "after push_heap: ";
    for (auto i : v) std::cout << i << ' ';
    std::cout << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
v: 9 5 4 1 1 3 
before push_heap: 9 5 4 1 1 3 6 
after push_heap:  9 5 6 1 1 3 4

二次

另见

pop_heap

removes the largest element from 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