Effective STL 31 Know your sorting options

来源:互联网 发布:单片机c51出租车计价器 编辑:程序博客网 时间:2024/06/07 19:45

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 satisfactoryvector<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 ordernth_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 widgetspartital_sort(widgets.begin(), widgets.begin() + 20, widgets.end(),                 qualityCompare);

V. sort
VI. stable_sort

原创粉丝点击