1. priority_queue
#include <queue>
默认大根堆!如果创建小根堆需要#include <functional> 类库
priority_queue<int, vector<int>, greater<int> > que1; //functional 头文件自带的 (greater),小根堆
priority_queue<int, vector<int>, less<int> > que2; //functional 头文件自带的
emplace函数:C++11新加入的函数,插入元素,和push功能相同,但是不需要内存移动,更高效
详细资料:http://www.cplusplus.com/reference/queue/priority_queue/
应用实例:POJ 3252 Fence Repair (霍夫曼树)
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <queue>
using namespace std;
long long cost;
int a, b;
int main() {
int N, len;
while (cin >> N) {
priority_queue<int, vector<int>, greater<int> > que;
cost = 0;
for (int i = 0; i < N; ++i) {
cin >> len;
que.push(len);
}
while (que.size() > 1) {
a = que.top();
que.pop();
b = que.top();
que.pop();
a = a + b;
cost += a;
que.push(a);
}
cout << cost << endl;
}
return 0;
}