64,648
社区成员
发帖
与我相关
我的任务
分享
template<class _RanIt,
class _Ty,
class _Pr> inline
void _Partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last,
_Pr _Pred, _Ty *)
{ // order [First, _Last) up to _Mid, using _Pred
_DEBUG_RANGE(_First, _Mid);
_DEBUG_RANGE(_Mid, _Last);
_DEBUG_POINTER(_Pred);
_STD make_heap(_First, _Mid, _Pred);
for (_RanIt _Next = _Mid; _Next < _Last; ++_Next) if (_DEBUG_LT_PRED(_Pred, *_Next, *_First)) { // replace top with new largest
_Ty _Val = _Move(*_Next);
_Pop_heap(_First, _Mid, _Next, _Move(_Val), _Pred,
_Dist_type(_First)); }
_STD sort_heap(_First, _Mid, _Pred);
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
const int testData[]={1,2,3,4,5,6,5,4,3,2,1,9,8,7,6,5,4,3,2,1,2,3,4,5,6,7,8,9,0};
struct printTop3
{
void operator()(const int &d)
{
cout<<d<<" ";
}
};
int main()
{
vector<int> testVec(testData,testData+29);
partial_sort(testVec.begin(),testVec.begin()+3,testVec.end(),greater<int>());
for_each(testVec.begin(),testVec.begin()+3,printTop3());
return 0;
}