泛型算法系列28:partial_sort()&&partial_sort_copy()&&partial_sum()

#include <algorithm> #include <vector> #include <iostream> #include <functional> /* * generates: original order of vector: 69 23 80 42 17 15 26 51 19 12 35 8 partial sort of vector: seven elements 8 12 15 17 19 23 26 80 69 51 42 35 partial_sort_copy() of first seven elements of vector in descending order 26 23 19 17 15 12 8 */ using namespace std; /************************************************************************/ /* */ template<class _RanIt> inline void my_partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last) { // order [First, _Last) up to _Mid, using operator< my_Partial_sort(_CHECKED_BASE(_First), _CHECKED_BASE(_Mid), _CHECKED_BASE(_Last), _Val_type(_First)); } template<class _RanIt, class _Ty> inline void my_Partial_sort(_RanIt _First, _RanIt _Mid, _RanIt _Last, _Ty *) { // order [First, _Last) up to _Mid, using operator< _DEBUG_RANGE(_First, _Mid); _DEBUG_RANGE(_Mid, _Last); std::make_heap(_First, _Mid);//use this range to heap for (_RanIt _Next = _Mid; _Next < _Last; ++_Next) if (_DEBUG_LT(*_Next, *_First)) _Pop_heap(_First, _Mid, _Next, _Ty(*_Next), _Dist_type(_First)); // replace top with new largest std::sort_heap(_First, _Mid); } /************************************************************************/ int main() { int ia[] = {69,23,80,42,17,15,26,51,19,12,35,8 }; vector< int > vec( ia, ia+12 ); ostream_iterator<int> out( cout," " ); cout << "original order of vector: "; copy( vec.begin(), vec.end(), out ); cout << endl; cout << "partial sort of vector: seven elements/n"; partial_sort( vec.begin(), vec.begin() + 7, vec.end() ); copy( vec.begin(), vec.end(), out ); cout << endl; vector< int > res(7); cout << "partial_sort_copy() of first seven elements/n/t" << "of vector in descending order /n"; partial_sort_copy( vec.begin(), vec.begin() + 7, res.begin(), res.end(), greater<int>() ); copy( res.begin(), res.end(), out ); cout << endl; copy( vec.begin(), vec.end(), out ); cout << endl; return 0; }

 

partial_sum()

#include <numeric> #include <vector> #include <iostream> #include <functional> using namespace std; /* * generates: elements: 1 3 4 5 7 8 9 partial sum of elements: 1 4 8 13 20 28 37 partial sum of elements using multiplies<int>(): 1 3 12 60 420 3360 30240 */ /************************************************************************/ /* */ template<class _InIt, class _OutIt, class _Fn2, class _Ty, class _InOutItCat> inline _OutIt my_Partial_sum(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn2 _Func, _Ty *, _InOutItCat, _Range_checked_iterator_tag) { // compute partial sums into _Dest, using _Func _DEBUG_RANGE(_First, _Last); _DEBUG_POINTER(_Dest); _DEBUG_POINTER(_Func); _Ty _Val = *_First; for (*_Dest = _Val; ++_First != _Last; *++_Dest = _Val) _Val = _Func(_Val, *_First); //_Val = _Val + *_First; <------no function version return (++_Dest); } template<class _InIt, class _OutIt, class _Fn2> inline _SCL_INSECURE_DEPRECATE _IF_NOT_CHK(_OutIt) my_partial_sum(_InIt _First, _InIt _Last, _OutIt _Dest, _Fn2 _Func) { // compute partial sums into _Dest, using _Func return (_First == _Last ? _Dest : my_Partial_sum(_CHECKED_BASE(_First), _CHECKED_BASE(_Last), _Dest, _Func, _Val_type(_First), _Iter_random(_First, _Dest), _STD _Range_checked_iterator_tag())); } /************************************************************************/ int main() { const int ia_size = 7; int ia[ ia_size ] = { 1, 3, 4, 5, 7, 8, 9 }; int ia_res[ ia_size ]; ostream_iterator< int > outfile( cout, " " ); vector< int > vec( ia, ia + ia_size ); vector< int > vec_res( vec.size() ); cout << "elements: "; copy( ia, ia + ia_size, outfile ); cout << endl; cout << "partial sum of elements:/n"; partial_sum( ia, ia+ia_size, ia_res ); copy( ia_res, ia_res + ia_size, outfile ); cout << endl; cout << "partial sum of elements using multiplies<int>():/n"; partial_sum( vec.begin(), vec.end(), vec_res.begin(), multiplies<int>() ); copy( vec_res.begin(), vec_res.end(), outfile ); cout << endl; return 0; }

你可能感兴趣的:(Algorithm,算法,vector,iterator,Random,Class)