C ++ STL 中的堆make_heap(),push_heap(),pop_heap(),sort_heap(),is_heap,is_heap_until()
堆是C ++或任何其他编程语言中最流行的数据结构之一。可以使用内置函数(如make_heap(),push_heap(),pop_heap(),sort_heap(),is_heap,is_heap_until())在堆中执行许多操作,例如删除,更新堆数据结构中的数据。在C ++语言中给出了堆数据结构中函数的实现如下。
C ++ 代码
// Here we are writing down the C++ Programming language code
// To demonstrate the concept of Heap in C++ STL | make_heap(),
//push_heap(),pop_heap(), sort_heap(), is_heap, is_heap_until()
#include
using namespace std;
//The main code functionality starts from here
int main()
{
// the below structure to create a vector is statically
// used to create the vector of elements listed below
vector v1 = {240, 360, 5440, 525, 115};
// the below small code snippet helps us with converting
// the to a heap from a vector using the C++ in built-in
// function make_heap()
make_heap(v1.begin(), v1.end());
// Displaying the maximum element of the heap
// using front()
// the below cout statements help us with displaying the
// maximum or the highest element that we have in our
// heap, which we have created
cout << "highest element in a heap created by us is: ";
cout << v1.front() << endl;
return 0; }
输出:
the highest element in a heap created by us is: 5440
C++代码
// Here we are writing down the C++ Programming language code
// To demonstrate the concept of Heap in C++ STL | make_heap(),
//push_heap(),pop_heap(), sort_heap(), is_heap, is_heap_until()
#include
using namespace std;
//The main code functionality starts from here
int main()
{
// the below structure to create a vector is statically
// used to create the vector of elements listed below
vector v1 = {2540, 6650, 4460, 2665, 9915};
// the below small code snippet helps us with converting
// the to a heap from a vector using the C++ in built-in
// function make_heap()
make_heap(v1.begin(), v1.end());
// the below cout statements help us with displaying the
// maximum or the highest element that we have in our
// heap, which we have created
cout << "highest element in the heap created by us is: ";
cout << v1.front() << endl;
// the below small cos=de snippet helps us with using
// the push_back() function in C++ to enter the elements
// in vector
v1.push_back(50);
// the below small code helps us with using the built-in
// push_heap() to
// reorder elements in the vector which we have created
push_heap(v1.begin(), v1.end());
// the below cout statements help us with displaying the
// maximum or the highest element that we have in our
// heap, which we have created after performing dot front
cout << "highest element in a heap after performing the operation is: ";
cout << v1.front() << endl;
// the below small code snippet helps us with
// using pop_heap() to delete maximum element
// from the heap which we have created
pop_heap(v1.begin(), v1.end());
v1.pop_back();
// the below cout statements help us with displaying the
// maximum or the highest element that we have in our
// heap, which we created after performing the pop
// operation in a heap is
cout << "highest element in the heap after performing pop operation is: ";
cout << v1.front() << endl;
return 0;
}
输出:
the highest element in a heap created by us is: 9915
the highest element in a heap after operating is: 9915
the highest element in a heap after performing the pop operation is: 6650
C++代码
// Here we are writing down the C++ Programming language code
// To demonstrate the concept of Heap in C++ STL | make_heap(),
//push_heap(),pop_heap(), sort_heap(), is_heap, is_heap_until()
#include
using namespace std;
//The main code functionality starts from here
int main()
{
// the below structure to create a vector is statically
// used to create the vector of elements listed below
vector v1 = {2540, 6650, 4460, 2665, 9915};
// the below small code snippet helps us with converting
// the to a heap from a vector using the C++ in built-in
// function make_heap()
make_heap(v1.begin(), v1.end());
// the below cout statements help us with displaying the
// maximum or the highest element that we have in our
// heap, which we have created
cout << "The heap elements are : ";
for (int &x : v1)
cout << x << " ";
cout << endl;
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: 9915 6650 4460 2665 2540
The heap elements after sorting are: 2540 2665 4460 6650 9915