他们的头文件函数是#include <algorithm>
make_heap()
函数原型:void make_heap(first_pointer,end_pointer,compare_function);
一个参数是数组或向量的头指针,
第二个向量是尾指针,
第三个参数是比较函数的名字。在缺省的时候,默认最大堆。
作用:把这一段的数组或向量做成一个堆的结构。范围是(first,last)
pop_heap()
函数原型:void pop_heap(first_pointer,end_pointer,compare_function);
作用:并不是真正的把最大元素从堆中弹出,而是重新排序堆。它把first和last-1交换,然后重新做成一个堆。可以使用容器的back来访问被“弹出“的元素或者使用pop_back来真正的删除。重载版本使用自定义的比较操作。
http://kb.cnblogs.com/a/1612665/中讲解了是该函数是如何工作的!
个人看法:很多资料都是上面的说法,那么这个函数应该也就是STL给我们提供的删除结点的方法,它应该不限于只交换first和last-1,如果我们想修改某个值,我们可以先用first获得指针,假设为p,然后调用pop_heap(p,last-1);然后将最后一个元素pop_back();通过跳进函数进行调试,发现在很短的时间内就能完成堆排序,但在删除任意函数时,并不像http://kb.cnblogs.com/a/1612665/中那样的算法进行删除的,但速度也很快,如果要究其实质,我觉得就得看STL源码分析了吧!哈哈,这个问题现在还没解决呢?还有为什么没有提供修改某一个元素的值,然后成堆的函数,还是堆不需要这些操作?
push_heap()
函数原型:void pushheap(first_pointer,end_pointer,compare_function);
作用:push_heap()假设由[first,last-1)是一个有效的堆,然后,再把堆中的新元素加
进来,做成一个堆。
sort_heap()void sort_heap(first_pointer,end_pointer,compare_function);
作用是sort_heap对[first,last)中的序列进行排序。它假设这个序列是有效堆。(当然,经过排序之后就不是一个有效堆了)
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
#include <map>
using namespace std;
void PrintInt(int elem){
cout << elem << ' ';
}
void PrintPair(const pair<string,int>& item){
cout << item.first << "[" << item.second << "] ";
}
bool bigger(const pair<string,int>& item1, const pair<string,int>& item2){
return item1.second > item2.second;
}
void TestVector(){
vector<int> coll;
int n;
while(cin >> n && n)
{
coll.push_back(n);
}
make_heap(coll.begin(), coll.end());
cout << "After make_heap()" << endl;
//for_each(coll.begin(), coll.end(), print);
copy(coll.begin(),coll.end(),ostream_iterator<int>(cout," "));
cout << endl;
cin >> n;
coll.push_back(n);
push_heap(coll.begin(), coll.end());
cout << "After push_heap()" << endl;
for_each(coll.begin(), coll.end(), PrintInt);
cout << endl;
pop_heap(coll.begin(), coll.end());
cout << "After pop_heap()" << endl;
for_each(coll.begin(), coll.end(), PrintInt);
cout << endl;
cout << "coll.back() : " << coll.back() << endl;
coll.pop_back();
sort_heap(coll.begin(), coll.end());
cout << "After sort_heap()" << endl;
for_each(coll.begin(), coll.end(), PrintInt);
cout << endl;
}
void TestPair(){
vector<pair<string, int>> vecPair;
/*vecPair.push_back(make_pair("aaa", 3));
vecPair.push_back(make_pair("bbb", 7));
vecPair.push_back(make_pair("ccc", 9));
vecPair.push_back(make_pair("ddd", 6));*/
string name;
int score;
while(cin >> name >> score){
//cout << name << " : " << score << endl;
//if(vecPair.size() == 0){
// vecPair.push_back(make_pair(name, score));
// continue;
//}
if(vecPair.size() > 0 && score < vecPair[0].second){
continue; //跳过小的
}
if(vecPair.size() < 3){
vecPair.push_back(make_pair(name, score));
push_heap(vecPair.begin(), vecPair.end(), bigger);
}
else{
vecPair[0] = make_pair(name, score);
make_heap(vecPair.begin(), vecPair.end(), bigger);
}
}
cout << "成绩最高的3个同学:" << endl;
for_each(vecPair.begin(), vecPair.end(), PrintPair);
cout << endl;
}
int main(){
TestPair();
system("pause");
return 0;
}