⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stl_algo.h

📁 c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出版社的光盘
💻 H
📖 第 1 页 / 共 5 页
字号:
    *__result_real_last = *__first;    ++__result_real_last;    ++__first;  }  make_heap(__result_first, __result_real_last, __comp);  while (__first != __last) {    if (__comp(*__first, *__result_first))      __adjust_heap(__result_first, _Distance(0),                    _Distance(__result_real_last - __result_first),                    _Tp(*__first),                    __comp);    ++__first;  }  sort_heap(__result_first, __result_real_last, __comp);  return __result_real_last;}template <class _InputIter, class _RandomAccessIter, class _Compare>inline _RandomAccessIterpartial_sort_copy(_InputIter __first, _InputIter __last,                  _RandomAccessIter __result_first,                  _RandomAccessIter __result_last, _Compare __comp) {  return __partial_sort_copy(__first, __last, __result_first, __result_last,                             __comp,                             __DISTANCE_TYPE(__result_first),                             __VALUE_TYPE(__first));}// nth_element() and its auxiliary functions.  template <class _RandomAccessIter, class _Tp>void __nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,                   _RandomAccessIter __last, _Tp*) {  while (__last - __first > 3) {    _RandomAccessIter __cut =      __unguarded_partition(__first, __last,                            _Tp(__median(*__first,                                         *(__first + (__last - __first)/2),                                         *(__last - 1))));    if (__cut <= __nth)      __first = __cut;    else       __last = __cut;  }  __insertion_sort(__first, __last);}template <class _RandomAccessIter>inline void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,                        _RandomAccessIter __last) {  __nth_element(__first, __nth, __last, __VALUE_TYPE(__first));}template <class _RandomAccessIter, class _Tp, class _Compare>void __nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,                   _RandomAccessIter __last, _Tp*, _Compare __comp) {  while (__last - __first > 3) {    _RandomAccessIter __cut =      __unguarded_partition(__first, __last,                            _Tp(__median(*__first,                                         *(__first + (__last - __first)/2),                                          *(__last - 1),                                         __comp)),                            __comp);    if (__cut <= __nth)      __first = __cut;    else       __last = __cut;  }  __insertion_sort(__first, __last, __comp);}template <class _RandomAccessIter, class _Compare>inline void nth_element(_RandomAccessIter __first, _RandomAccessIter __nth,                 _RandomAccessIter __last, _Compare __comp) {  __nth_element(__first, __nth, __last, __VALUE_TYPE(__first), __comp);}// Binary search (lower_bound, upper_bound, equal_range, binary_search).template <class _ForwardIter, class _Tp, class _Distance>_ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,                           const _Tp& __val, _Distance*) {  _Distance __len = 0;  distance(__first, __last, __len);  _Distance __half;  _ForwardIter __middle;  while (__len > 0) {    __half = __len >> 1;    __middle = __first;    advance(__middle, __half);    if (*__middle < __val) {      __first = __middle;      ++__first;      __len = __len - __half - 1;    }    else      __len = __half;  }  return __first;}template <class _ForwardIter, class _Tp>inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last,                                   const _Tp& __val) {  return __lower_bound(__first, __last, __val,                       __DISTANCE_TYPE(__first));}template <class _ForwardIter, class _Tp, class _Compare, class _Distance>_ForwardIter __lower_bound(_ForwardIter __first, _ForwardIter __last,                              const _Tp& __val, _Compare __comp, _Distance*){  _Distance __len = 0;  distance(__first, __last, __len);  _Distance __half;  _ForwardIter __middle;  while (__len > 0) {    __half = __len >> 1;    __middle = __first;    advance(__middle, __half);    if (__comp(*__middle, __val)) {      __first = __middle;      ++__first;      __len = __len - __half - 1;    }    else      __len = __half;  }  return __first;}template <class _ForwardIter, class _Tp, class _Compare>inline _ForwardIter lower_bound(_ForwardIter __first, _ForwardIter __last,                                const _Tp& __val, _Compare __comp) {  return __lower_bound(__first, __last, __val, __comp,                       __DISTANCE_TYPE(__first));}template <class _ForwardIter, class _Tp, class _Distance>_ForwardIter __upper_bound(_ForwardIter __first, _ForwardIter __last,                           const _Tp& __val, _Distance*){  _Distance __len = 0;  distance(__first, __last, __len);  _Distance __half;  _ForwardIter __middle;  while (__len > 0) {    __half = __len >> 1;    __middle = __first;    advance(__middle, __half);    if (__val < *__middle)      __len = __half;    else {      __first = __middle;      ++__first;      __len = __len - __half - 1;    }  }  return __first;}template <class _ForwardIter, class _Tp>inline _ForwardIter upper_bound(_ForwardIter __first, _ForwardIter __last,                                const _Tp& __val) {  return __upper_bound(__first, __last, __val,                       __DISTANCE_TYPE(__first));}template <class _ForwardIter, class _Tp, class _Compare, class _Distance>_ForwardIter __upper_bound(_ForwardIter __first, _ForwardIter __last,                           const _Tp& __val, _Compare __comp, _Distance*){  _Distance __len = 0;  distance(__first, __last, __len);  _Distance __half;  _ForwardIter __middle;  while (__len > 0) {    __half = __len >> 1;    __middle = __first;    advance(__middle, __half);    if (__comp(__val, *__middle))      __len = __half;    else {      __first = __middle;      ++__first;      __len = __len - __half - 1;    }  }  return __first;}template <class _ForwardIter, class _Tp, class _Compare>inline _ForwardIter upper_bound(_ForwardIter __first, _ForwardIter __last,                                const _Tp& __val, _Compare __comp) {  return __upper_bound(__first, __last, __val, __comp,                       __DISTANCE_TYPE(__first));}template <class _ForwardIter, class _Tp, class _Distance>pair<_ForwardIter, _ForwardIter>__equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,              _Distance*){  _Distance __len = 0;  distance(__first, __last, __len);  _Distance __half;  _ForwardIter __middle, __left, __right;  while (__len > 0) {    __half = __len >> 1;    __middle = __first;    advance(__middle, __half);    if (*__middle < __val) {      __first = __middle;      ++__first;      __len = __len - __half - 1;    }    else if (__val < *__middle)      __len = __half;    else {      __left = lower_bound(__first, __middle, __val);      advance(__first, __len);      __right = upper_bound(++__middle, __first, __val);      return pair<_ForwardIter, _ForwardIter>(__left, __right);    }  }  return pair<_ForwardIter, _ForwardIter>(__first, __first);}template <class _ForwardIter, class _Tp>inline pair<_ForwardIter, _ForwardIter>equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val) {  return __equal_range(__first, __last, __val,                       __DISTANCE_TYPE(__first));}template <class _ForwardIter, class _Tp, class _Compare, class _Distance>pair<_ForwardIter, _ForwardIter>__equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,              _Compare __comp, _Distance*){  _Distance __len = 0;  distance(__first, __last, __len);  _Distance __half;  _ForwardIter __middle, __left, __right;  while (__len > 0) {    __half = __len >> 1;    __middle = __first;    advance(__middle, __half);    if (__comp(*__middle, __val)) {      __first = __middle;      ++__first;      __len = __len - __half - 1;    }    else if (__comp(__val, *__middle))      __len = __half;    else {      __left = lower_bound(__first, __middle, __val, __comp);      advance(__first, __len);      __right = upper_bound(++__middle, __first, __val, __comp);      return pair<_ForwardIter, _ForwardIter>(__left, __right);    }  }  return pair<_ForwardIter, _ForwardIter>(__first, __first);}           template <class _ForwardIter, class _Tp, class _Compare>inline pair<_ForwardIter, _ForwardIter>equal_range(_ForwardIter __first, _ForwardIter __last, const _Tp& __val,            _Compare __comp) {  return __equal_range(__first, __last, __val, __comp,                       __DISTANCE_TYPE(__first));} template <class _ForwardIter, class _Tp>bool binary_search(_ForwardIter __first, _ForwardIter __last,                   const _Tp& __val) {  _ForwardIter __i = lower_bound(__first, __last, __val);  return __i != __last && !(__val < *__i);}template <class _ForwardIter, class _Tp, class _Compare>bool binary_search(_ForwardIter __first, _ForwardIter __last,                   const _Tp& __val,                   _Compare __comp) {  _ForwardIter __i = lower_bound(__first, __last, __val, __comp);  return __i != __last && !__comp(__val, *__i);}// merge, with and without an explicitly supplied comparison function.template <class _InputIter1, class _InputIter2, class _OutputIter>_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,                  _InputIter2 __first2, _InputIter2 __last2,                  _OutputIter __result) {  while (__first1 != __last1 && __first2 != __last2) {    if (*__first2 < *__first1) {      *__result = *__first2;      ++__first2;    }    else {      *__result = *__first1;      ++__first1;    }    ++__result;  }  return copy(__first2, __last2, copy(__first1, __last1, __result));}template <class _InputIter1, class _InputIter2, class _OutputIter,          class _Compare>_OutputIter merge(_InputIter1 __first1, _InputIter1 __last1,                  _InputIter2 __first2, _InputIter2 __last2,                  _OutputIter __result, _Compare __comp) {  while (__first1 != __last1 && __first2 != __last2) {    if (__comp(*__first2, *__first1)) {      *__result = *__first2;      ++__first2;    }    else {      *__result = *__first1;      ++__first1;    }    ++__result;  }  return copy(__first2, __last2, copy(__first1, __last1, __result));}// inplace_merge and its auxiliary functions. template <class _BidirectionalIter, class _Distance>void __merge_without_buffer(_BidirectionalIter __first,                            _BidirectionalIter __middle,                            _BidirectionalIter __last,                            _Distance __len1, _Distance __len2) {  if (__len1 == 0 || __len2 == 0)    return;  if (__len1 + __len2 == 2) {    if (*__middle < *__first)      iter_swap(__first, __middle);    return;  }  _BidirectionalIter __first_cut = __first;  _BidirectionalIter __second_cut = __middle;  _Distance __len11 = 0;  _Distance __len22 = 0;  if (__len1 > __len2) {    __len11 = __len1 / 2;    advance(__first_cut, __len11);    __second_cut = lower_bound(__middle, __last, *__first_cut);    distance(__middle, __second_cut, __len22);  }  else {    __len22 = __len2 / 2;    advance(__second_cut, __len22);    __first_cut = upper_bound(__first, __middle, *__second_cut);    distance(__first, __first_cut, __len11);  }  _BidirectionalIter __new_middle    = rotate(__first_cut, __middle, __second_cut);  __merge_without_buffer(__first, __first_cut, __new_middle,                         __len11, __len22);  __merge_without_buffer(__new_middle, __second_cut, __last, __len1 - __len11,                         __len2 - __len22);}template <class _BidirectionalIter, class _Distance, class _Compare>void __merge_without_buffer(_BidirectionalIter __first,                            _BidirectionalIter __middle,                            _BidirectionalIter __last,                            _Distance __len1, _Distance __len2,                            _Compare __comp) {  if (__len1 == 0 || __len2 == 0)    return;  if (__len1 + __len2 == 2) {    if (__comp(*__middle, *__first))      iter_swap(__first, __middle);    return;  }  _BidirectionalIter __first_cut = __first;  _BidirectionalIter __second_cut = __middle;  _Distance __len11 = 0;  _Distance __len22 = 0;  if (__len1 > __len2) {    __len11 = __len1 / 2;    advance(__first_cut, __len11);    __second_cut = lower_bound(__middle, __last, *__first_cut, __comp);    distance(__middle, __second_cut, __len22);  }  else {    __len22 = __len2 / 2;    advance(__second_cut, __len22);    __first_cut = upper_bound(__first, __middle, *__second_cut, __comp);    distance(__first, __first_cut, __len11);  }  _BidirectionalIter __new_middle    = rotate(__first_cut, __middle, __second_cut);  __merge_without_buffer(__first, __first_cut, __new_middle, __len11, __len22,                         __comp);  __merge_without_buffer(__new_middle, _

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -