stl minheap
This commit is contained in:
parent
31eb852495
commit
1769ed1b1f
|
@ -0,0 +1,27 @@
|
|||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
int main() {
|
||||
std::vector<int> v = {3, 5, 1, 2, 9, 0, 6, 15};
|
||||
|
||||
//* init
|
||||
std::make_heap(v.begin(), v.end(), std::greater<int>());
|
||||
|
||||
//* pop
|
||||
std::cout << "1: " << v.front() << '\n';
|
||||
std::pop_heap(v.begin(), v.end(), std::greater<int>());
|
||||
v.pop_back();
|
||||
|
||||
//* push
|
||||
v.push_back(666);
|
||||
std::push_heap(v.begin(), v.end(), std::greater<int>());
|
||||
|
||||
int i = 2;
|
||||
while (v.size()) {
|
||||
std::cout << i++ << ": " << v.front() << '\n';
|
||||
std::pop_heap(v.begin(), v.end(), std::greater<int>());
|
||||
v.pop_back();
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue