int data[] = {3,54,254,52,13,667,234,67,256,78,467,32,65,324,889,34,5};
int len = sizeof(data)/sizeof(int);
vector<int> v1(data,data+len);
ostream_iterator<int> out(cout," ");
copy(v1.begin(),v1.end(),out);
sort(v1.begin(),v1.end(),greater<int>());
copy(v1.begin(),v1.end(),out);
LessThan(int val):value(val){}
int data[] = {3,54,254,52,475,667,234,67,256,78,467,32,65,324,889,34,5};
int len = sizeof(data)/sizeof(int);
list<int> list1(data,data+k);
ostream_iterator<int> out(cout," ");
list1.sort(greater<int>());//对前K个数进行排序
pos = find_if(list1.begin(),list1.end(),LessThan(data[i]));//找到第一个比要插入值小的元素位置
list1.insert(pos,data[i]);//插入新值
list1.pop_back();//删除最后一个多余的元素
copy(list1.begin(),list1.end(),out);
cout<<"第"<<k<<"大的数是: "<<endl;
for(pos = list1.begin();pos!=list1.end();++pos,++count)
void HeapAdjust(vector<T> &v,size_t start,size_t end)
for(size_t j=(2*s+1);j<=end;j=(2*j+1))
void HeapSort(vector<T> &v)
for(i=(v.size()-1)/2;i>=0;--i)
HeapAdjust(v,i,v.size()-1);
for(i=v.size(),j=k;i>1&&j>0;--i,--j)
int data[]={93,5,233,55,3,67,2,67,32,6,89,355};
int len = sizeof(data)/sizeof(int);
vector<int> v1(data,data+len);
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/03/31/1131862.html,如需转载请自行联系原作者