Effective STL 31 Know your sorting options

with algorithms that do more work tend to use fewer resources listed before those that requir more:
I. partition sort

bool hasAcceptableQaulity(const Widfet& w) {
    // return whether w has a quality rating of 2 or better;
}

// move all widgets satisfying hasAcceptableQaulity to the front of widgets, 
// and return an iterator to the first widget that isn't satisfactory
vector<Widget>::iterator goodEnd = partition(widgets.begin(), widgets.end(), 
                                            hasAcceptableQaulity);

II. stable_partition
III. nth_element
其功能是对区间 [_First, _Last) 的元素进行重排,其中位于位置 _Nth 的元素与整个区间排序后位于位置 _Nth 的元素相同,并且满足在位置 _Nth 之前的所有元素都“不大于”它和位置 _Nth 之后的所有元素都“不小于”它,而且并不保证 _Nth 的前后两个区间的所有元素保持有序。

template<class _RanIt, class _Pr>  
void nth_element(_RanIt _First, _RanIt _Nth, _RanIt _Last, _Pr _Pred);
bool qualityCompare(const Widget& lhs, const Widget& rhs) {
    // return lh's quality is greater than rh's quality
}

// put the best 20 elements at the front of widgets, but do not worry about t
// their order
nth_element(widgets.begin(), widgets.begin() + 19, widgets.end(), 
            qualityCompare);

IV. partial_sort

bool qualityCompare(const Widget& lhs, const Widget& rhs) {
    // return lh's quality is greater than rh's quality
}

// put the best 20 elements (in order) at the front of widgets
partital_sort(widgets.begin(), widgets.begin() + 20, widgets.end(), 
                qualityCompare);

V. sort
VI. stable_sort

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值