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

📅  最后修改于: 2021-05-30 19:51:49             🧑  作者: Mango

堆数据结构可以使用STL在一定范围内实现,该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() :-此函数用于检查容器是否为。通常,在大多数实现中,反向排序的容器被视为堆。如果容器是堆,则返回true;否则返回false。

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等的更多准备工作,请参阅“完整面试准备课程”