📜  C++ STL 中的堆 | make_heap()、push_heap()、pop_heap()、sort_heap()、is_heap、is_heap_until()

📅  最后修改于: 2021-10-28 02:03:54             🧑  作者: Mango

堆数据结构可以在使用 STL 的范围内实现,这允许更快地输入到堆中,并且检索一个数字总是导致最大的数字,即每次弹出最大数量的剩余数字。堆的其他数量根据实现进行安排。

堆上的操作

1. make_heap() :- 此函数用于容器中的范围转换为堆。

2. front() :- 此函数显示堆的第一个元素,即最大数量

// C++ code to demonstrate the working of 
// make_heap(), front()
#include 
using namespace std;
int main()
{
      
    // Initializing a vector
    vector v1 = {20, 30, 40, 25, 15};
      
    // Converting vector into a heap
    // using make_heap()
    make_heap(v1.begin(), v1.end());
      
    // Displaying the maximum element of heap
    // using front()
    cout << "The maximum element of heap is : ";
    cout << v1.front() << endl;
      
    return 0;
}

输出:

The maximum element of heap is : 40

3. push_heap() :- 该函数用于向堆中插入元素。堆的大小增加1。新元素适当地放置在堆中。

4. pop_heap() :- 该函数用于删除的最大元素。堆的大小减 1。此操作后,堆元素会相应地重新组织。

// C++ code to demonstrate the working of 
// push_heap() and pop_heap()
#include 
using namespace std;
int main()
{
      
    // Initializing a vector
    vector v1 = {20, 30, 40, 25, 15};
      
    // Converting vector into a heap
    // using make_heap()
    make_heap(v1.begin(), v1.end());
      
    // Displaying the maximum element of heap
    // using front()
    cout << "The maximum element of heap is : ";
    cout << v1.front() << endl;
      
    // using push_back() to enter element
    // in vector
    v1.push_back(50);
      
    // using push_heap() to reorder elements
    push_heap(v1.begin(), v1.end());
      
    // Displaying the maximum element of heap
    // using front()
    cout << "The maximum element of heap after push is : ";
    cout << v1.front() << endl;
      
     // using pop_heap() to delete maximum element
    pop_heap(v1.begin(), v1.end());
    v1.pop_back();
      
    // Displaying the maximum element of heap
    // using front()
    cout << "The maximum element of heap after pop is : ";
    cout << v1.front() << endl;
      
    return 0;
}

输出:

The maximum element of heap is : 40
The maximum element of heap after push is : 50
The maximum element of heap after pop is : 40

5. sort_heap() :- 该函数用于堆进行排序。此操作后,容器不再是堆

// C++ code to demonstrate the working of 
// sort_heap()
#include 
using namespace std;
int main()
{
      
    // Initializing a vector
    vector v1 = {20, 30, 40, 25, 15};
      
    // Converting vector into a heap
    // using make_heap()
    make_heap(v1.begin(), v1.end());
      
    // Displaying heap elements 
    cout << "The heap elements are : ";
    for (int &x : v1) 
       cout << x << " ";
    cout << endl;
      
    // sorting heap using sort_heap()
    sort_heap(v1.begin(), v1.end());
      
     // Displaying heap elements 
    cout << "The heap elements after sorting are : ";
    for (int &x : v1) 
       cout << x << " ";
      
    return 0;
}

输出:

The heap elements are : 40 30 20 25 15 
The heap elements after sorting are : 15 20 25 30 40 

6. is_heap() :- 该函数用于检查容器是否为。通常,在大多数实现中,反向排序的容器被视为堆。如果容器是堆则返回真,否则返回假。

6. is_heap_until() :- 该函数将迭代器返回到位置,直到容器成为堆。通常,在大多数实现中,反向排序的容器被视为堆。

// C++ code to demonstrate the working of 
// is_heap() and is_heap_until()
#include 
using namespace std;
int main()
{
      
    // Initializing a vector
    vector v1 = {40, 30, 25, 35, 15};
      
    // Declaring heap iterator
    vector::iterator it1;
      
    // Checking if container is heap
    // using is_heap()
    is_heap(v1.begin(), v1.end())?
    cout << "The container is heap ":
    cout << "The container is not heap";
    cout << endl;
      
    // using is_heap_until() to check position 
    // till which container is heap
    auto it = is_heap_until(v1.begin(), v1.end());
    
    // Displaying heap range elements
    cout << "The heap elements in container are : ";
    for (it1=v1.begin(); it1!=it; it1++)
       cout << *it1 << " ";
     
    return 0;
}

输出:

The container is not heap
The heap elements in container are : 40 30 25 
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程