有10亿个浮点数,求出其中最大的10000个 ,用了标准库的,不让用的话,只能自己写堆函数
vector<float> bigs(10000,0);
vector<float>::iterator it;
for(it=bigs.begin();it!=bigs.end();it++)
{
*it = (float)rand()/7; //数据都是用随机数模拟的
}
cout<<bigs.size()<<endl;
make_heap(bigs.begin(),bigs.end(),greater<float>() );
float ff;
time_t t1,t2;
time(&t1);
for(int i=0;i<1000000000;i++)
{
ff = (float) rand()/7;
if(ff>bigs[0])
{
pop_heap(bigs.begin(),bigs.end(),greater<float>());
bigs.pop_back();
bigs.push_back(ff);
push_heap(bigs.begin(),bigs.end(),greater<float>());
}
}
time(&t2);
cout<<(long)(t2-t1)<<endl;