#include <algorithm> #include <vector> #include <iostream> #include <functional> using namespace std; /* * generates: original element sequence: 29 23 20 22 17 15 26 51 19 12 35 40 stable_partition on even element: 20 22 26 12 40 29 23 17 15 51 19 stable_partition of less than 25: 23 20 22 17 15 19 12 29 26 51 35 40 */ class even_elem { public: bool operator()( int elem ) { return elem % 2 ? false : true; } }; /************************************************************************/ /* */ template<class _Fn1> class my_binder1st : public unary_function<typename _Fn1::second_argument_type, typename _Fn1::result_type> { public: typedef typename unary_function<typename _Fn1::second_argument_type, typename _Fn1::result_type>::argument_type argument_type; typedef typename unary_function<typename _Fn1::second_argument_type, typename _Fn1::result_type>::result_type result_type; my_binder1st(const _Fn1& _Func, const typename _Fn1::first_argument_type& _Left) : op(_Func), value(_Left) { // construct from functor and right operand } result_type operator()(argument_type& _Right) const { return (op(value, _Right)); } result_type operator()(const argument_type& _Right) const { return (op(value, _Right)); } protected: _Fn1 op; typename _Fn1::first_argument_type value; }; template<class _Fn1, class _Ty> inline my_binder1st<_Fn1> my_bind1st(const _Fn1& _Func, const _Ty& _Left) { // return a binder2nd functor adapter typename _Fn1::first_argument_type _Val(_Left); return (my_binder1st<_Fn1>(_Func, _Val)); } /************************************************************************/ int main() { int ia[] = { 29,23,20,22,17,15,26,51,19,12,35,40 }; vector< int > vec( ia, ia + 12 ); ostream_iterator< int > ofile( cout, " " ); cout << "original element sequence:/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; stable_partition( &ia[0], &ia[12], even_elem() ); //partition( &ia[0], &ia[12], even_elem() ); cout << "stable_partition on even element:/n"; copy( ia, ia + 11, ofile ); cout << '/n'; vector< int >::iterator iter = stable_partition( vec.begin(), vec.end(), /*bind2nd(less<int>(),25)*/my_bind1st(greater<int>(),25) ); //delete the value range which is greater than 25 vec.erase(iter,vec.end()); cout << "stable_partition of less than 25:/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; return 0; } /************************************************************************/ /* template<class _Arg1, class _Arg2, class _Result> struct binary_function { // base class for binary functions typedef _Arg1 first_argument_type; typedef _Arg2 second_argument_type; typedef _Result result_type; }; template<class _Arg, class _Result> struct unary_function { // base class for unary functions typedef _Arg argument_type; typedef _Result result_type; }; */ template<class _Fn2, class _Ty> inline binder2nd<_Fn2> my_bind2nd(const _Fn2& _Func, const _Ty& _Right) { // return a binder2nd functor adapter typename _Fn2::second_argument_type _Val(_Right); return (my_binder2nd<_Fn2>(_Func, _Val)); } template<class _Fn2> class my_binder2nd : public unary_function<typename _Fn2::first_argument_type, typename _Fn2::result_type> { // functor adapter _Func(left, stored) public: typedef unary_function<typename _Fn2::first_argument_type, typename _Fn2::result_type> _Base; typedef typename _Base::argument_type argument_type; typedef typename _Base::result_type result_type; my_binder2nd(const _Fn2& _Func, const typename _Fn2::second_argument_type& _Right) : op(_Func), value(_Right) { // construct from functor and right operand } result_type operator()(const argument_type& _Left) const { // apply functor to operands return (op(_Left, value)); } result_type operator()(argument_type& _Left) const { // apply functor to operands return (op(_Left, value)); } protected: _Fn2 op; // the functor to apply typename _Fn2::second_argument_type value; // the right operand }; /************************************************************************/
#include <algorithm> #include <vector> #include <iostream> #include <functional> using namespace std; /* * generates: original element sequence: 29 23 20 22 12 17 15 26 51 19 12 23 35 40 stable sort -- default ascending order: 12 12 15 17 19 20 22 23 23 26 29 35 40 51 stable sort: descending order: 51 40 35 29 26 23 23 22 20 19 17 15 12 12 */ /************************************************************************/ /* */ template<class _BidIt, class _Diff, class _Ty> inline void my_Stable_sort(_BidIt _First, _BidIt _Last, _Diff _Count, _Temp_iterator<_Ty>& _Tempbuf) { // sort preserving order of equivalents, using operator< if (_Count <= _ISORT_MAX) std::_Insertion_sort(_First, _Last); // small else { // sort halves and merge _Diff _Count2 = (_Count + 1) / 2; _BidIt _Mid = _First; std::advance(_Mid, _Count2); if (_Count2 <= _Tempbuf._Maxlen()) { // temp buffer big enough, sort each half using buffer _Buffered_merge_sort(_First, _Mid, _Count2, _Tempbuf); _Buffered_merge_sort(_Mid, _Last, _Count - _Count2, _Tempbuf); } else { // temp buffer not big enough, divide and conquer _Stable_sort(_First, _Mid, _Count2, _Tempbuf); _Stable_sort(_Mid, _Last, _Count - _Count2, _Tempbuf); } _Buffered_merge(_First, _Mid, _Last, _Count2, _Count - _Count2, _Tempbuf); // merge sorted halves } } template<class _BidIt> inline void my_stable_sort(_BidIt _First, _BidIt _Last) { // sort preserving order of equivalents, using operator< iterator_traits<_BidIt>::difference_type _Count = 0; typedef typename iterator_traits<_BidIt>::value_type value_type; _Distance(_First, _Last, _Count); _Temp_iterator<value_type> _Tempbuf((_Count + 1) / 2); my_Stable_sort(_First, _Last, _Count, _Tempbuf); } template<class _RanIt, class _Diff> inline void my_Sort(_RanIt _First, _RanIt _Last, _Diff _Ideal) { // order [_First, _Last), using operator< _Diff _Count; for (; _ISORT_MAX < (_Count = _Last - _First) && 0 < _Ideal; ) { // divide and conquer by quicksort pair<_RanIt, _RanIt> _Mid = std::_Unguarded_partition(_First, _Last); _Ideal /= 2, _Ideal += _Ideal / 2; // allow 1.5 log2(N) divisions if (_Mid.first - _First < _Last - _Mid.second) { // loop on second half std::_Sort(_First, _Mid.first, _Ideal); _First = _Mid.second; } else { // loop on first half std::_Sort(_Mid.second, _Last, _Ideal); _Last = _Mid.first; } } if (_ISORT_MAX < _Count) { // heap sort if too many divisions std::make_heap(_First, _Last); std::sort_heap(_First, _Last); } else if (1 < _Count) std::_Insertion_sort(_First, _Last); // small } template<class _RanIt> inline void my_sort(_RanIt _First, _RanIt _Last) { // order [_First, _Last), using operator< _DEBUG_RANGE(_First, _Last); std::my_Sort(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Last - _First); } /************************************************************************/ int main() { int ia[] = { 29,23,20,22,12,17,15,26,51,19,12,23,35,40 }; vector< int > vec( ia, ia + 14 ); ostream_iterator< int > ofile( cout, " " ); cout << "original element sequence:/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; my_stable_sort( &ia[0], &ia[14] ); cout << "stable sort -- default ascending order: /n"; copy( ia, ia + 14, ofile ); cout << '/n'; stable_sort( vec.begin(), vec.end(), greater<int>() ); cout << "stable sort: descending order: /n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; return 0; }
函数名 | 功能描述 |
---|---|
sort | 对给定区间所有元素进行排序 |
stable_sort | 对给定区间所有元素进行稳定排序 |
partial_sort | 对给定区间所有元素部分排序 |
partial_sort_copy | 对给定区间复制并排序 |
nth_element | 找出给定区间的某个位置对应的元素 |
is_sorted | 判断一个区间是否已经排好序 |
partition | 使得符合某个条件的元素放在前面 |