Simple use of C++ STL heap correlation function
A brief introduction to the use of heap related functions
Heap is not a container, but a special way to organize container elements.
Only the scope of the heap can be determined, that is, the scope specified by the start and end iterators.
This means that you can create a heap with subsequences of elements in the container, and you can add elements to the generated heap.
Take the vector container as an example:
To simplify the code, the read and output of vector are packed into two functions
vector<int> get_input() { int n; cin>>n; vector<int> nums; for(int i=0; i<n; i++) { int x; cin>>x; nums.insert(nums.end(), x); } return nums; } void out(vector<int> nums) { for(int i=0; i<nums.size(); i++) { cout<<nums[i]<<" "; } cout<<endl; }
1. create heap
/* function : Organize the data in the sequence container of the specified address segment into heaps according to the comparison rules param 1 : Starting address of sequential container param 2 : End address of the sequential container param 3 : Compare function, use less < > by default, that is, to create a large top heap return : --- */ make_heap(param1, param2, param3);
Main function: use greater < int > to create a small top heap
make_heap(nums.begin(), nums.end(), greater<int>());
Result:
2. Add elements to the heap
Be careful:
- The comparison rule at insert time (i.e. param3, comparison function) must be the same as at heap creation time, or a heap can only use the same comparison rule
- The new element should be the last element, so you only need to call the push back of the corresponding container, and then call the push heap to insert the new element into the push
/* function : Inserts [last] data from the sequence container of the specified address segment into the previous heap param 1 : Starting address of sequential container param 2 : End address of the sequential container param 3 : Compare function, use less < > by default, i.e. insert large top heap return : --- */ push_heap(param1, param2, param3);
Main function: insert a new element into the organized heap
make_heap(nums.begin(), nums.end(), greater<int>()); nums.push_back(6); push_heap(nums.begin(), nums.end(), greater<int>());
Result:
3. Delete the top element
Be careful
- Because there is an adjustment action after removing the heap top element, it requires that the comparison rule (i.e. param3, comparison function) must be the same as when the heap is created, or that a heap can only use the same comparison rule
- Pop heap only exchanges the elements to the end, but does not move out of the container. Finally, you need to call the pop back of the container to remove the last element
/* function : Put the [first] data in the sequence container of the specified address segment last, and then reorganize the previous data into a heap param 1 : Starting address of sequential container param 2 : End address of the sequential container param 3 : Compare function, use less < > by default, delete and adjust large top heap return : --- */ pop_heap(param1, param2, param3);
Main function: remove top element
make_heap(nums.begin(), nums.end(), greater<int>()); nums.push_back(6); push_heap(nums.begin(), nums.end(), greater<int>()); pop_heap(nums.begin(), nums.end(), greater<int>()); nums.pop_back();
Result:
4. Judge whether the sequence is a heap
Be careful:
When a function is called, the comparison rule (param3, comparison function) must be the same as when the heap is created, or a heap can only use the same comparison rule
/* function : Determine whether the sequence container satisfies the properties of the heap param 1 : Starting address of sequential container param 2 : End address of the sequential container param 3 : Compare function, using less < > by default, to determine whether the large heap is satisfied return : true or false */ is_heap(param1, param2, param3);
5. heap sort
By constantly exchanging the heap top elements to the end of the sequence, and then adjusting the front heap, because the heap top elements have polarity (maximum or minimum), the final sequence is a sorted sequence
- If it's a large top heap, it's sorted in ascending order
- If it's a small top heap, it's sorted in descending order
/* function : Heap the sequence container of the specified address segment param 1 : Starting address of sequential container param 2 : End address of the sequential container param 3 : The comparison function uses less < > by default. After sorting the big top heap, it is in ascending order return : --- */ sort_heap(param1, param2, param3);
Main function: heap sort
make_heap(nums.begin(), nums.end(), greater<int>()); sort_heap(nums.begin(), nums.end(), greater<int>());
Result: the small top heap created is sorted in descending order
Complete code
#include <iostream> #include <algorithm> #include <vector> using namespace std; vector<int> get_input() { int n; cin>>n; vector<int> nums; for(int i=0; i<n; i++) { int x; cin>>x; nums.insert(nums.end(), x); } return nums; } void out(vector<int> nums) { for(int i=0; i<nums.size(); i++) { cout<<nums[i]<<" "; } cout<<endl; } int main() { vector<int> nums = get_input(); /* Fill in the code in the [main function] section above */ out(nums); return 0; }