鎺㈢储STL锛氬爢鏁版嵁缁撴瀯鍙婄畻娉?/h1> Posted Algorithms
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎺㈢储STL锛氬爢鏁版嵁缁撴瀯鍙婄畻娉?/h1>
鍗拌薄涓€绘槸瑙夊緱 heap
鏁版嵁缁撴瀯鏄湪STL涓殑涓€涓爣鍑嗙粍浠娥煠旓紝鎯充簡瑙e涔犱竴涓婼TL鍙堟槸浠ユ€庢牱涓€绉嶅阀濡欏厖婊¤璁℃劅鐨勬柟娉曫煒叉潵瀹屾垚鐨勶紝浣嗚繜杩熸病鏈夋壘鍒板畠鍒板簳鍦ㄥ摢閲屾斁鐫€馃锛岄殢鐫€鏃堕棿鎺ㄧЩ涔熷氨瀵瑰叾涓嶄簡浜嗕箣馃槍馃ケ馃槾銆備粖澶╂墠鍙戠幇瀹冨苟涓嶆槸馃槷锛屽畠娌℃湁浠ユā鏉跨被鐨勬柟寮忔彁渚涙帴鍙o紝浣嗘槸鍗翠互鍙﹀涓€绉嶆洿鍔犻€氱敤鍜屼究鎼虹殑鏂瑰紡灞曠幇鍏剁畻娉曠簿楂撱€?/p>
浠婂ぉ鍚冪殑楸兼憜鎽嗛拡涓嶆埑~馃悷馃馃惉馃惓馃悑銆?/p>
浠ヤ笅涓?heap
鐩稿叧鐨勫疄鐜扮畻娉曟嫹璐濊嚜
GUN ISO C++ Library Copyright (C) 2001-2018 Free Software Foundation, Inc.
鍏跺疄鍦紙 priority_queue
锛変腑宸茬粡鐢ㄥ埌瀹冧簡锛岃繖涓鍣ㄥ厑璁哥敤鎴蜂互浠绘剰椤哄簭鏀惧叆瀹瑰櫒鍐咃紝浣嗘槸鍙栧嚭鏉ョ殑鏃跺€欙紝涓€瀹氫細鎶婃渶楂樹紭鍏堢骇鐨勫厓绱犲厛鍙栧嚭鏉ャ€傝繖閲屽畠鐢ㄥ埌鐨勬槸 binary-max-heap
浣滀负 priority_queue
鐨勫簳灞傜壒鎬с€?/p>
鍦ㄥ簳灞傝〃绀轰笂锛?code class="mq-16">binary-max-heap 灏辨槸涓€涓畬鍏ㄤ簩鍙夋爲锛岃€屼笖浣跨敤涓€涓竴缁存暟缁勬潵鏉ヨ〃绀恒€傞櫎姝や箣澶栵紝瀹冨皢绗竴涓綅缃繚鐣欎笅鏉ヤ笉浣跨敤锛岃繖鏍峰氨鍙互淇濊瘉涔嬪悗鐨勫瓙鑺傜偣鍜岀埗鑺傜偣鏈夎繖鏍风殑鍏崇郴锛?/p>
-
瀛愯妭鐐?
鐨勭埗鑺傜偣涓€瀹氭槸
-
鐖惰妭鐐?
鐨勫乏瀛愯妭鐐逛竴瀹氭槸
锛屽彸瀛愯妭鐐逛竴瀹氭槸
杩欎箞涓€鏉ワ紝灏辨柟渚垮浜嗮煒夈€?/p>
瑙e喅浜嗚妭鐐圭储寮曢棶棰橈紝杩樻湁涓€涓棶棰樻槸瀛樺偍闂锛岀敱浜?heap
鏄敮鎸佸姩鎬佹彃鍏ュ垹闄ゅ厓绱犵殑锛屾墍浠ユ渶濂芥槸鐢?vector
鏉ヤ綔涓哄簳灞傛暟鎹粨鏋勩€?/p>
闄ゆ涔嬪锛?code class="mq-40">heap 鏄湁涓ょ鏍煎紡鐨勶紝涓€绉嶆槸澶ф牴鍫嗭紙max-heap锛夛紝鍙︿竴绉嶆槸灏忔牴鍫嗭紙min-heap锛夛紝鍓嶈€呮剰鍛崇潃鍫嗕腑鏈€澶у厓绱犲湪鏍硅妭鐐逛笂锛屾瘡涓€妫靛瓙鏍戠殑鏍硅妭鐐规槸瑕佸ぇ浜庡畠鐨勫乏鍙冲瓙鑺傜偣鐨勶紝鍚庤€呮伆鎭扮浉鍙嶃€傚湪STL涓粯璁ゆ彁渚涚殑鏄ぇ鏍瑰爢锛屼篃灏辨槸浼樺厛绾э紙鎴栬€呬唬琛ㄤ紭鍏堢骇鏈€楂樼殑閿€硷級鏈€澶х殑鍦ㄦ牴鑺傜偣涓娿€?/p>
1. heap 绠楁硶
浠ヤ笅鍧囧亣璁?heap
浣跨敤 vector
鏉ュ瓨鍌ㄥ厓绱狅紝鑰屼笖绗竴涓綅缃笉鐢ㄣ€?/p>
1.1 push-heap
绠楁硶
绠€鑰岃█涔嬶細鍏堟妸鍏冪礌鏀惧埌 vector
鏈熬 v.end()
锛岀劧鍚庡悜涓婅皟鏁达紝鎵ц涓婃函绋嬪簭锛?/p>
灏嗘柊鑺傜偣鎷挎潵涓庡叾鐖惰妭鐐规瘮杈冿紝濡傛灉鍏朵紭鍏堢骇姣旂埗鍏冪礌澶э紝閭d箞灏辫皟鎹袱鑰呬綅缃紝濡傛鍙嶅鐩村埌鏍硅妭鐐规垨鑰呬笉鐢ㄥ啀璋冦€?/p>
鍏蜂綋婧愮爜濡備笅锛?/p>
// 杩欎釜瀹炵幇涓紝涓嶆敮鎸佽嚜瀹氫箟姣旇緝鍑芥暟锛岀洿鎺ヤ娇鐢ㄩ瀹氫箟鐨勬瘮杈冨嚱鏁?nbsp;cmp
template<typename _RandomAccessIterator>
inline void
push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
typedef typename iterator_traits<_RandomAccessIterator>::value_type
_ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type
_DistanceType;
// 姣旇緝鍑芥暟锛岃涓嬫枃
__gnu_cxx::__ops::_Iter_less_val __comp;
_ValueType __value = _GLIBCXX_MOVE(*(__last - 1)); // 鍏堝彇鍑烘潵鏈€鍚庝竴涓厓绱?/span>
std::__push_heap(__first, _DistanceType((__last - __first) - 1),
_DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
}
// 杩欎釜瀹炵幇涓紝鏀寔浜嗚嚜瀹氫箟姣旇緝鍑芥暟 cmp
template<typename _RandomAccessIterator, typename _Compare>
inline void
push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
_Compare __comp) {
// 瀹氫箟浜嗕竴鍫嗘湁鐨勬病鐨勶紝蹇界暐骞朵笉濡ㄧ鐞嗚В涓嬫枃
typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
// 鎵惧埌姣旇緝鍑芥暟骞跺紩鍏ヨ繘鏉?/span>
__decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp)))
__cmp(_GLIBCXX_MOVE(__comp));
// 鑾峰彇鏈€鍚庣殑閭d釜鍊?/span>
_ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
std::__push_heap(__first, _DistanceType((__last - __first) - 1),
_DistanceType(0), _GLIBCXX_MOVE(__value), __cmp);
}
鍦ㄩ槄璇昏繃绋嬩腑锛屽彲浠ョ湅鍒版湁濂藉涓?cmp
锛屼粠瀛楅潰涓婁笉闅剧悊瑙f槸涓€涓敤鏉ユ瘮杈冪殑鍑芥暟 compare
锛屽畠鐨勫畾涔夊湪 predefined_ops.h
涓紝鍏蜂綋濡備笅锛?/p>
struct _Iter_less_iter {
template<typename _Iterator1, typename _Iterator2>
// 姣旇緝鍑芥暟锛屽洜涓烘槸 < 鎵€浠ユ渶缁堟帓搴忕粨鏋滀細鏄粠澶у埌灏?/span>
bool operator()(_Iterator1 __it1, _Iterator2 __it2) const {
return *__it1 < *__it2;
}
};
鍐嶅洖鍒颁笂闈㈢殑鍏蜂綋瀹炵幇涓幓锛屽畠浠湪鏈€鍚庨兘璋冪敤浜嗕竴涓?__push_heap()
鐨勫嚱鏁帮紝杩欎釜鍑芥暟鏄湡姝e畬鎴愬叆鍫嗘搷浣滅殑鍑芥暟锛屼箣鎵€浠ュ啓鎴愯繖鏍峰氨鏄负浜嗛殣钘忔帴鍙o紝瀹冪殑鍏蜂綋瀹炵幇濡備笅锛?/p>
// 鐪熸瀹屾垚鎿嶄綔鐨勫嚱鏁帮紝鎻愪緵缁欎笅闈袱涓?nbsp;push_heap 鏉ヨ皟鐢?/span>
template<typename _RandomAccessIterator, typename _Distance, typename _Tp,
typename _Compare>
void
__push_heap(_RandomAccessIterator __first,
_Distance __holeIndex, _Distance __topIndex, _Tp __value,
_Compare &__comp) {
// 鎵惧埌鐖惰妭鐐逛綅缃?/span>
_Distance __parent = (__holeIndex - 1) / 2;
// 濡傛灉褰撳墠鑺傜偣杩樻病鍒伴《鐐瑰锛岃€屼笖鍏冪礌姣旇緝缁撴灉浠嶄笉绗﹀悎瑕佹眰锛堜紭鍏堢骇澶х殑鍦ㄤ笅闈級
while (__holeIndex > __topIndex && __comp(__first + __parent, __value)) {
// 璋冩暣鐖惰妭鐐瑰拰褰撳墠瀛愯妭鐐圭殑鍏冪礌鍐呭
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
__holeIndex = __parent;
__parent = (__holeIndex - 1) / 2;
}
// 鎵惧埌浜嗗厓绱犵殑浣嶇疆锛岀劧鍚庢斁涓嬫潵
*(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
}
1.2 pop_heap
绠楁硶
杩欎釜杩囩▼鍙氨娌℃湁 push_heap
杩囩▼绠€鍗曚簡銆傚畠鍏堟妸鏍硅妭鐐癸紝涔熷氨鏄?code class="mq-141">vector 涓殑绗竴涓厓绱犲彇璧帮紙鍏跺疄鏄斁鍒颁簡鏈锛夛紝鍚屾椂鎶婃渶鍚庝竴涓厓绱犲彇鍑烘潵銆傜劧鍚庢墽琛?span>涓嬫函绋嬪簭锛?/p>
灏嗙┖闂磋妭鐐瑰拰鍏惰緝澶у瓙鑺傜偣瀵硅皟锛屽苟鎸佺画涓嬫斁锛岀洿鍒板彾瀛愯妭鐐广€?/p>
杩欎釜杩囩▼瀹屾垚鐨勬槸鎶婄┖鍑烘潵鐨勭涓€涓綅缃~鍏呬笂鍚堥€傜殑鏍硅妭鐐广€?/p>
鎵ц瀹屼箣鍚庯紝鍐嶆墽琛?span>涓婃函绋嬪簭锛岃繖涓繃绋嬪畬鎴愮殑鏄妸涔嬪墠鍙栧嚭鏉ョ殑鏈€鍚庝竴涓厓绱犳斁鍥炲埌鍚堥€傜殑浣嶇疆鍘汇€?/p>
涓嬮潰鏄畠鐨勫疄鐜扮粏鑺傦細
template<typename _RandomAccessIterator>
inline void
pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
// 鍒犳帀浜嗕竴浜涘苟涓嶅奖鍝嶉槄璇荤殑绋嬪簭
if (__last - __first > 1) {
// 棣栧厛锛岀缉鍑忕┖闂存寚绀哄櫒
--__last;
// 鐒跺悗锛屽鍏ラ粯璁ょ殑姣旇緝鍑芥暟锛岃涓婃枃
__gnu_cxx::__ops::_Iter_less_iter __comp;
// 璋冪敤鐪熸鐨勫叆鍫嗗嚱鏁?/span>
std::__pop_heap(__first, __last, __last, __comp);
}
}
template<typename _RandomAccessIterator, typename _Compare>
inline void
pop_heap(_RandomAccessIterator __first,
_RandomAccessIterator __last, _Compare __comp) {
// 杩樻槸鍒犳帀浜嗕竴浜涘苟涓嶅奖鍝嶉槄璇荤殑绋嬪簭
if (__last - __first > 1) {
// 寮曞叆杩涙潵姣旇緝鍑芥暟锛岀敤鎴疯嚜瀹氫箟鐨勫櫌~
typedef __decltype(__comp)
_Cmp;
__gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
// 缂╁噺绌洪棿鎸囩ず鍣?/span>
--__last;
// 鏀惧埌鍫嗛噷
std::__pop_heap(__first, __last, __last, __cmp);
}
}
鍏跺疄鐪嬪埌 __pop_heap()
锛屼笉闅剧寽娴嬪拰 push_heap()
涓€鏍风殑鎿嶄綔锛氬畾涔変簡涓€涓€滅瀵嗏€濅絾鈥滅湡姝b€濆畬鎴愭妸鍏冪礌鏀惧埌鍫嗛噷闈㈢殑鎿嶄綔銆傚涓嬶細
template<typename _RandomAccessIterator, typename _Compare>
inline void
__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
_RandomAccessIterator __result, _Compare &__comp) {
// 瀹氫箟浜嗕竴浜涙棤鍏崇揣瑕佺殑绋嬪簭锛屼笉鐪嬪苟涓嶅奖鍝嶉槄璇?/span>
typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
// 鍙栧嚭鏉ユ湯灏惧€硷紙__last == __result锛?/span>
_ValueType __value = _GLIBCXX_MOVE(*__result);
// 鐒跺悗鎶婃湯灏惧€肩疆鎴愰閮ㄥ€?/span>
*__result = _GLIBCXX_MOVE(*__first);
// 姝ゆ椂鍫嗗ご宸茬粡涓嶅啀鏄渶灏忓€间簡锛岃€屾槸鏈€鍒濈殑鏈熬鍊硷紝鎵€浠ラ渶瑕佽皟鏁存暣涓爢
std::__adjust_heap(__first, _DistanceType(0),
_DistanceType(__last - __first),
_GLIBCXX_MOVE(__value), __comp);
}
鐪嬪埌杩欓噷锛屽氨鍙堢姱鎰佷簡馃槪锛岃繖涓?__adjust_heap()
鍑芥暟鏄釜浠€涔堜笢涓滐紵杩欎釜 __adjust_heap()
鍑芥暟灏辨槸鐢ㄦ潵璋冩暣鏁翠釜鍫嗙殑鍑芥暟锛屽畬鎴愪簡涓婃函鍜?span>涓嬫函鎿嶄綔锛屽涓嬶細
template<typename _RandomAccessIterator, typename _Distance,
typename _Tp, typename _Compare>
void
__adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
_Distance __len, _Tp __value, _Compare __comp) {
const _Distance __topIndex = __holeIndex; // 璁颁笅鏉ユ礊鑺傜偣鎵€鎸囧悜鐨勮妭鐐?/span>
_Distance __secondChild = __holeIndex; // 鐢ㄦ潵鎸囧悜娲炶妭鐐瑰彸杈圭殑鑺傜偣
while (__secondChild < (__len - 1) / 2) {
// 鎵惧嚭鏂版礊鑺傜偣鐨勫彸鑺傜偣
__secondChild = 2 * (__secondChild + 1);
// 姣旇緝涓€涓嬫礊鑺傜偣宸﹀彸涓よ竟鐨勫€硷紝鐒跺悗鐢?nbsp;__secondChild 鎸囧悜杈冨ぇ鐨勫€?/span>
if (__comp(__first + __secondChild, __first + (__secondChild - 1))) __secondChild--;
// 涓嬫函杩囩▼锛屾洿鏂版礊鑺傜偣鐨勫€硷紝浣夸箣鎴愪负杈冨ぇ鍊?/span>
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
// 鐒跺悗鍐嶆妸娲炲彿涓嬬Щ鍒拌緝澶ц妭鐐瑰
__holeIndex = __secondChild;
}
// 濡傛灉娌℃湁鍙冲瓙鑺傜偣锛屽彧鏈夊乏瀛愯妭鐐?/span>
if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2) {
__secondChild = 2 * (__secondChild + 1);
// 璁╁乏瀛愯妭鐐逛綅娲炲€硷紝鐒跺悗鍐嶈娲炲彿涓嬬Щ鍒板乏瀛愯妭鐐瑰
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + (__secondChild - 1)));
__holeIndex = __secondChild - 1;
}
// 瀵煎叆姣旇緝鍑芥暟涔嬬被鐨勬潅涓冩潅鍏?/span>
__decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp)))
__cmp(_GLIBCXX_MOVE(__comp));
// 鍐嶈繘琛屼竴娆′笂婧搷浣?/span>
std::__push_heap(__first, __holeIndex, __topIndex, _GLIBCXX_MOVE(__value), __cmp);
}
鍏跺疄鏁翠釜鍒犻櫎鍫嗛《鍏冪礌鐨勬搷浣滃埌姝ゅ氨缁撴潫浜嗭紝浣嗘槸锛佸洖杩囧ご鏉ョ湅鐪嬶紝鍏跺疄璇ュ厓绱犲苟娌℃湁鐪熸琚垹闄ゆ帀锛岃€屾槸鏀惧湪鍫嗙殑鏈熬缁х画瀛樺湪鐫€锛岄渶瑕佷汉涓鸿繘琛屽垹闄わ紝楠岃瘉濡備笅锛?/p>
int main() {
vector<int> iv = {0, 1, 2, 3, 4, 8, 9, 3, 5};
make_heap(iv.begin(), iv.end());
cout << iv << endl; // 9 5 8 3 4 0 2 3 1
pop_heap(iv.begin(), iv.end());
cout << iv.back() << endl; // 9
cout << iv << endl; // 8 7 4 3 5 0 2 3 1 銆?銆?/span>
}
涓轰粈涔堣繖鏍凤紵涓轰粈涔堜笉鐩存帴鍒犻櫎鎺夐噴鏀剧┖闂达紵璇风户缁湅涓嬮潰鐨?sort_heap
绠楁硶銆?/p>
1.3 sort_heap
绠楁硶
缁撳悎 pop_heap
绠楁硶涓嶉毦鐪嬪嚭锛屾瘡涓€娆$殑 pop_heap
鎿嶄綔閮戒細鎶婂綋鍓嶅爢鐨勬渶澶у€兼墧鍒?vector
瀹瑰櫒灏鹃儴鍘伙紝閭d箞鍙互杩欐牱鑰冭檻锛氭寔缁鏁翠釜 heap
杩涜 pop_heap
鎿嶄綔锛岄偅涔堟瘡娆″皢鎿嶄綔鑼冨洿浠庡悗鍚戝墠鍙互缂╁噺涓€涓厓绱狅紝褰撹杩囩▼鎵ц瀹屽悗锛屽氨鍙互寰楀埌涓€涓湁搴忓簭鍒楋紝渚嬪锛?/p>
int main() {
vector<int> iv = {0, 1, 2, 3, 4, 8, 9, 3, 5};
make_heap(iv.begin(), iv.end());
sort_heap(iv.begin(), iv.end());
cout << iv << endl; // 0 1 2 3 3 4 5 7 8 9
}
鎴戜滑鏉ョ湅鐪嬫槸鎬庢牱瀹炵幇鐨勶細
template<typename _RandomAccessIterator, typename _Compare>
void
__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare &__comp) {
while (__last - __first > 1) {
// 涓嶅仠鍦板幓 pop_heap 锛屽悓鏃惰鎶婃湯灏炬寚閽堜笉鏂墠绉?/span>
--__last;
std::__pop_heap(__first, __last, __last, __comp);
}
}
template<typename _RandomAccessIterator>
inline void
sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
// 鍒犳帀浜嗕竴浜涘鐞嗚В鏁翠釜绋嬪簭鏃犲叧绱ц鐨勬鏌ョ▼搴?/span>
__gnu_cxx::__ops::_Iter_less_iter __comp;
std::__sort_heap(__first, __last, __comp);
}
template<typename _RandomAccessIterator, typename _Compare>
inline void
sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
// 鎴戝張鍒犳帀浜嗕竴浜涘鐞嗚В鏁翠釜绋嬪簭鏃犲叧绱ц鐨勬鏌ョ▼搴?/span>
typedef __decltype(__comp) _Cmp;
__gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
std::__sort_heap(__first, __last, __cmp);
}
绠€鍗曟槑浜嗭紝灏辨槸 鈥滀笉鍋滃湴鍘?pop_heap 锛屽悓鏃惰鎶婃湯灏炬寚閽堜笉鏂墠绉烩€濔煒濄€?/p>
1.4 make_heap
绠楁硶
灏嗕竴涓幇鏈夋暟鎹浆鎹㈡垚涓€涓?heap
锛屽叾涓昏渚濇嵁灏辨槸瀹屽叏浜屽弶鏍戠殑闅愬紡琛ㄨ堪銆傚叿浣撶畻娉曞疄鐜扮粏鑺傚涓嬶細
template<typename _RandomAccessIterator, typename _Compare>
void __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare &__comp) {
typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
// 濡傛灉鏁翠釜搴忓垪澶у皬閮戒笉瓒?2锛岄偅鎺掑畠骞插暐锛燄煒?/span>
if (__last - __first < 2) return;
// 璁板綍涓嬫潵闇€瑕佹搷浣滅殑鑼冨洿
const _DistanceType __len = __last - __first;
// 鎵惧埌鏈熬鍏冪礌鐨勭埗鑺傜偣
_DistanceType __parent = (__len - 2) / 2;
while (true) {
// 閲嶆柊鎺掑竷浠?nbsp;parent 涓洪鐨勫瓙鏍戯紝
_ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value), __comp
);
// 濡傛灉璧板畬浜嗘牴鑺傜偣锛岄偅涔堝氨缁撴潫杩斿洖
if (__parent == 0) return;
// 宸茬粡鎷嶅ソ搴忕殑瀛愭爲澶村悜鍓嶇Щ鍔ㄤ竴涓寚閽?/span>
__parent--;
}
}
2. 涓€浜涗緥瀛?/h1>
绠€鍗曟槑浜嗭紝鐪嬬湅灏卞ソ馃槈
/**
* Created by Xiaozhong on 2020/11/21.
* Copyright (c) 2020/11/21 Xiaozhong. All rights reserved.
*/
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
ostream &operator<<(ostream &os, const vector<int> &v) {
for (auto i : v) os << i << " ";
return os;
}
int main() {
vector<int> iv = {0, 1, 2, 3, 4, 8, 9, 3, 5};
make_heap(iv.begin(), iv.end());
cout << iv << endl; // 9 5 8 3 4 0 2 3 1
iv.push_back(7);
push_heap(iv.begin(), iv.end());
cout << iv << endl; // 9 7 8 3 5 0 2 3 1 4
pop_heap(iv.begin(), iv.end());
cout << iv.back() << endl; // 9
cout << iv << endl; // 8 7 4 3 5 0 2 3 1 銆?銆?/span>
iv.pop_back();
sort_heap(iv.begin(), iv.end());
cout << iv << endl; // 0 1 2 3 3 4 5 7 8
}
以上是关于鎺㈢储STL锛氬爢鏁版嵁缁撴瀯鍙婄畻娉?/h1>
Posted Algorithms
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鎺㈢储STL锛氬爢鏁版嵁缁撴瀯鍙婄畻娉?/h1>
鍗拌薄涓€绘槸瑙夊緱 heap
鏁版嵁缁撴瀯鏄湪STL涓殑涓€涓爣鍑嗙粍浠娥煠旓紝鎯充簡瑙e涔犱竴涓婼TL鍙堟槸浠ユ€庢牱涓€绉嶅阀濡欏厖婊¤璁℃劅鐨勬柟娉曫煒叉潵瀹屾垚鐨勶紝浣嗚繜杩熸病鏈夋壘鍒板畠鍒板簳鍦ㄥ摢閲屾斁鐫€馃锛岄殢鐫€鏃堕棿鎺ㄧЩ涔熷氨瀵瑰叾涓嶄簡浜嗕箣馃槍馃ケ馃槾銆備粖澶╂墠鍙戠幇瀹冨苟涓嶆槸馃槷锛屽畠娌℃湁浠ユā鏉跨被鐨勬柟寮忔彁渚涙帴鍙o紝浣嗘槸鍗翠互鍙﹀涓€绉嶆洿鍔犻€氱敤鍜屼究鎼虹殑鏂瑰紡灞曠幇鍏剁畻娉曠簿楂撱€?/p>
浠婂ぉ鍚冪殑楸兼憜鎽嗛拡涓嶆埑~馃悷馃馃惉馃惓馃悑銆?/p>
浠ヤ笅涓?
heap
鐩稿叧鐨勫疄鐜扮畻娉曟嫹璐濊嚜
GUN ISO C++ Library Copyright (C) 2001-2018 Free Software Foundation, Inc.
鍏跺疄鍦紙 priority_queue
锛変腑宸茬粡鐢ㄥ埌瀹冧簡锛岃繖涓鍣ㄥ厑璁哥敤鎴蜂互浠绘剰椤哄簭鏀惧叆瀹瑰櫒鍐咃紝浣嗘槸鍙栧嚭鏉ョ殑鏃跺€欙紝涓€瀹氫細鎶婃渶楂樹紭鍏堢骇鐨勫厓绱犲厛鍙栧嚭鏉ャ€傝繖閲屽畠鐢ㄥ埌鐨勬槸 binary-max-heap
浣滀负 priority_queue
鐨勫簳灞傜壒鎬с€?/p>
鍦ㄥ簳灞傝〃绀轰笂锛?code class="mq-16">binary-max-heap 灏辨槸涓€涓畬鍏ㄤ簩鍙夋爲锛岃€屼笖浣跨敤涓€涓竴缁存暟缁勬潵鏉ヨ〃绀恒€傞櫎姝や箣澶栵紝瀹冨皢绗竴涓綅缃繚鐣欎笅鏉ヤ笉浣跨敤锛岃繖鏍峰氨鍙互淇濊瘉涔嬪悗鐨勫瓙鑺傜偣鍜岀埗鑺傜偣鏈夎繖鏍风殑鍏崇郴锛?/p>
-
瀛愯妭鐐? 鐨勭埗鑺傜偣涓€瀹氭槸 -
鐖惰妭鐐? 鐨勫乏瀛愯妭鐐逛竴瀹氭槸 锛屽彸瀛愯妭鐐逛竴瀹氭槸
杩欎箞涓€鏉ワ紝灏辨柟渚垮浜嗮煒夈€?/p>
瑙e喅浜嗚妭鐐圭储寮曢棶棰橈紝杩樻湁涓€涓棶棰樻槸瀛樺偍闂锛岀敱浜?heap
鏄敮鎸佸姩鎬佹彃鍏ュ垹闄ゅ厓绱犵殑锛屾墍浠ユ渶濂芥槸鐢?vector
鏉ヤ綔涓哄簳灞傛暟鎹粨鏋勩€?/p>
闄ゆ涔嬪锛?code class="mq-40">heap 鏄湁涓ょ鏍煎紡鐨勶紝涓€绉嶆槸澶ф牴鍫嗭紙max-heap锛夛紝鍙︿竴绉嶆槸灏忔牴鍫嗭紙min-heap锛夛紝鍓嶈€呮剰鍛崇潃鍫嗕腑鏈€澶у厓绱犲湪鏍硅妭鐐逛笂锛屾瘡涓€妫靛瓙鏍戠殑鏍硅妭鐐规槸瑕佸ぇ浜庡畠鐨勫乏鍙冲瓙鑺傜偣鐨勶紝鍚庤€呮伆鎭扮浉鍙嶃€傚湪STL涓粯璁ゆ彁渚涚殑鏄ぇ鏍瑰爢锛屼篃灏辨槸浼樺厛绾э紙鎴栬€呬唬琛ㄤ紭鍏堢骇鏈€楂樼殑閿€硷級鏈€澶х殑鍦ㄦ牴鑺傜偣涓娿€?/p>
1. heap 绠楁硶
浠ヤ笅鍧囧亣璁?heap
浣跨敤 vector
鏉ュ瓨鍌ㄥ厓绱狅紝鑰屼笖绗竴涓綅缃笉鐢ㄣ€?/p>
1.1 push-heap
绠楁硶
绠€鑰岃█涔嬶細鍏堟妸鍏冪礌鏀惧埌 灏嗘柊鑺傜偣鎷挎潵涓庡叾鐖惰妭鐐规瘮杈冿紝濡傛灉鍏朵紭鍏堢骇姣旂埗鍏冪礌澶э紝閭d箞灏辫皟鎹袱鑰呬綅缃紝濡傛鍙嶅鐩村埌鏍硅妭鐐规垨鑰呬笉鐢ㄥ啀璋冦€?/p>
鍏蜂綋婧愮爜濡備笅锛?/p>
鍦ㄩ槄璇昏繃绋嬩腑锛屽彲浠ョ湅鍒版湁濂藉涓? 鍐嶅洖鍒颁笂闈㈢殑鍏蜂綋瀹炵幇涓幓锛屽畠浠湪鏈€鍚庨兘璋冪敤浜嗕竴涓? 杩欎釜杩囩▼鍙氨娌℃湁 灏嗙┖闂磋妭鐐瑰拰鍏惰緝澶у瓙鑺傜偣瀵硅皟锛屽苟鎸佺画涓嬫斁锛岀洿鍒板彾瀛愯妭鐐广€?/p>
杩欎釜杩囩▼瀹屾垚鐨勬槸鎶婄┖鍑烘潵鐨勭涓€涓綅缃~鍏呬笂鍚堥€傜殑鏍硅妭鐐广€?/p>
鎵ц瀹屼箣鍚庯紝鍐嶆墽琛?span>涓婃函绋嬪簭锛岃繖涓繃绋嬪畬鎴愮殑鏄妸涔嬪墠鍙栧嚭鏉ョ殑鏈€鍚庝竴涓厓绱犳斁鍥炲埌鍚堥€傜殑浣嶇疆鍘汇€?/p>
涓嬮潰鏄畠鐨勫疄鐜扮粏鑺傦細 鍏跺疄鐪嬪埌 鐪嬪埌杩欓噷锛屽氨鍙堢姱鎰佷簡馃槪锛岃繖涓? 鍏跺疄鏁翠釜鍒犻櫎鍫嗛《鍏冪礌鐨勬搷浣滃埌姝ゅ氨缁撴潫浜嗭紝浣嗘槸锛佸洖杩囧ご鏉ョ湅鐪嬶紝鍏跺疄璇ュ厓绱犲苟娌℃湁鐪熸琚垹闄ゆ帀锛岃€屾槸鏀惧湪鍫嗙殑鏈熬缁х画瀛樺湪鐫€锛岄渶瑕佷汉涓鸿繘琛屽垹闄わ紝楠岃瘉濡備笅锛?/p>
涓轰粈涔堣繖鏍凤紵涓轰粈涔堜笉鐩存帴鍒犻櫎鎺夐噴鏀剧┖闂达紵璇风户缁湅涓嬮潰鐨? 缁撳悎 鎴戜滑鏉ョ湅鐪嬫槸鎬庢牱瀹炵幇鐨勶細 绠€鍗曟槑浜嗭紝灏辨槸 鈥滀笉鍋滃湴鍘?pop_heap 锛屽悓鏃惰鎶婃湯灏炬寚閽堜笉鏂墠绉烩€濔煒濄€?/p>
灏嗕竴涓幇鏈夋暟鎹浆鎹㈡垚涓€涓? 绠€鍗曟槑浜嗭紝鐪嬬湅灏卞ソ馃槈vector
鏈熬 v.end()
锛岀劧鍚庡悜涓婅皟鏁达紝鎵ц涓婃函绋嬪簭锛?/p>
// 杩欎釜瀹炵幇涓紝涓嶆敮鎸佽嚜瀹氫箟姣旇緝鍑芥暟锛岀洿鎺ヤ娇鐢ㄩ瀹氫箟鐨勬瘮杈冨嚱鏁?nbsp;cmp
template<typename _RandomAccessIterator>
inline void
push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
typedef typename iterator_traits<_RandomAccessIterator>::value_type
_ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type
_DistanceType;
// 姣旇緝鍑芥暟锛岃涓嬫枃
__gnu_cxx::__ops::_Iter_less_val __comp;
_ValueType __value = _GLIBCXX_MOVE(*(__last - 1)); // 鍏堝彇鍑烘潵鏈€鍚庝竴涓厓绱?/span>
std::__push_heap(__first, _DistanceType((__last - __first) - 1),
_DistanceType(0), _GLIBCXX_MOVE(__value), __comp);
}
// 杩欎釜瀹炵幇涓紝鏀寔浜嗚嚜瀹氫箟姣旇緝鍑芥暟 cmp
template<typename _RandomAccessIterator, typename _Compare>
inline void
push_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
_Compare __comp) {
// 瀹氫箟浜嗕竴鍫嗘湁鐨勬病鐨勶紝蹇界暐骞朵笉濡ㄧ鐞嗚В涓嬫枃
typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
// 鎵惧埌姣旇緝鍑芥暟骞跺紩鍏ヨ繘鏉?/span>
__decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp)))
__cmp(_GLIBCXX_MOVE(__comp));
// 鑾峰彇鏈€鍚庣殑閭d釜鍊?/span>
_ValueType __value = _GLIBCXX_MOVE(*(__last - 1));
std::__push_heap(__first, _DistanceType((__last - __first) - 1),
_DistanceType(0), _GLIBCXX_MOVE(__value), __cmp);
}cmp
锛屼粠瀛楅潰涓婁笉闅剧悊瑙f槸涓€涓敤鏉ユ瘮杈冪殑鍑芥暟 compare
锛屽畠鐨勫畾涔夊湪 predefined_ops.h
涓紝鍏蜂綋濡備笅锛?/p>
struct _Iter_less_iter {
template<typename _Iterator1, typename _Iterator2>
// 姣旇緝鍑芥暟锛屽洜涓烘槸 < 鎵€浠ユ渶缁堟帓搴忕粨鏋滀細鏄粠澶у埌灏?/span>
bool operator()(_Iterator1 __it1, _Iterator2 __it2) const {
return *__it1 < *__it2;
}
};__push_heap()
鐨勫嚱鏁帮紝杩欎釜鍑芥暟鏄湡姝e畬鎴愬叆鍫嗘搷浣滅殑鍑芥暟锛屼箣鎵€浠ュ啓鎴愯繖鏍峰氨鏄负浜嗛殣钘忔帴鍙o紝瀹冪殑鍏蜂綋瀹炵幇濡備笅锛?/p>
// 鐪熸瀹屾垚鎿嶄綔鐨勫嚱鏁帮紝鎻愪緵缁欎笅闈袱涓?nbsp;push_heap 鏉ヨ皟鐢?/span>
template<typename _RandomAccessIterator, typename _Distance, typename _Tp,
typename _Compare>
void
__push_heap(_RandomAccessIterator __first,
_Distance __holeIndex, _Distance __topIndex, _Tp __value,
_Compare &__comp) {
// 鎵惧埌鐖惰妭鐐逛綅缃?/span>
_Distance __parent = (__holeIndex - 1) / 2;
// 濡傛灉褰撳墠鑺傜偣杩樻病鍒伴《鐐瑰锛岃€屼笖鍏冪礌姣旇緝缁撴灉浠嶄笉绗﹀悎瑕佹眰锛堜紭鍏堢骇澶х殑鍦ㄤ笅闈級
while (__holeIndex > __topIndex && __comp(__first + __parent, __value)) {
// 璋冩暣鐖惰妭鐐瑰拰褰撳墠瀛愯妭鐐圭殑鍏冪礌鍐呭
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __parent));
__holeIndex = __parent;
__parent = (__holeIndex - 1) / 2;
}
// 鎵惧埌浜嗗厓绱犵殑浣嶇疆锛岀劧鍚庢斁涓嬫潵
*(__first + __holeIndex) = _GLIBCXX_MOVE(__value);
}1.2
pop_heap
绠楁硶push_heap
杩囩▼绠€鍗曚簡銆傚畠鍏堟妸鏍硅妭鐐癸紝涔熷氨鏄?code class="mq-141">vector 涓殑绗竴涓厓绱犲彇璧帮紙鍏跺疄鏄斁鍒颁簡鏈锛夛紝鍚屾椂鎶婃渶鍚庝竴涓厓绱犲彇鍑烘潵銆傜劧鍚庢墽琛?span>涓嬫函绋嬪簭锛?/p>
template<typename _RandomAccessIterator>
inline void
pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
// 鍒犳帀浜嗕竴浜涘苟涓嶅奖鍝嶉槄璇荤殑绋嬪簭
if (__last - __first > 1) {
// 棣栧厛锛岀缉鍑忕┖闂存寚绀哄櫒
--__last;
// 鐒跺悗锛屽鍏ラ粯璁ょ殑姣旇緝鍑芥暟锛岃涓婃枃
__gnu_cxx::__ops::_Iter_less_iter __comp;
// 璋冪敤鐪熸鐨勫叆鍫嗗嚱鏁?/span>
std::__pop_heap(__first, __last, __last, __comp);
}
}
template<typename _RandomAccessIterator, typename _Compare>
inline void
pop_heap(_RandomAccessIterator __first,
_RandomAccessIterator __last, _Compare __comp) {
// 杩樻槸鍒犳帀浜嗕竴浜涘苟涓嶅奖鍝嶉槄璇荤殑绋嬪簭
if (__last - __first > 1) {
// 寮曞叆杩涙潵姣旇緝鍑芥暟锛岀敤鎴疯嚜瀹氫箟鐨勫櫌~
typedef __decltype(__comp)
_Cmp;
__gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
// 缂╁噺绌洪棿鎸囩ず鍣?/span>
--__last;
// 鏀惧埌鍫嗛噷
std::__pop_heap(__first, __last, __last, __cmp);
}
}__pop_heap()
锛屼笉闅剧寽娴嬪拰 push_heap()
涓€鏍风殑鎿嶄綔锛氬畾涔変簡涓€涓€滅瀵嗏€濅絾鈥滅湡姝b€濆畬鎴愭妸鍏冪礌鏀惧埌鍫嗛噷闈㈢殑鎿嶄綔銆傚涓嬶細template<typename _RandomAccessIterator, typename _Compare>
inline void
__pop_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
_RandomAccessIterator __result, _Compare &__comp) {
// 瀹氫箟浜嗕竴浜涙棤鍏崇揣瑕佺殑绋嬪簭锛屼笉鐪嬪苟涓嶅奖鍝嶉槄璇?/span>
typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
// 鍙栧嚭鏉ユ湯灏惧€硷紙__last == __result锛?/span>
_ValueType __value = _GLIBCXX_MOVE(*__result);
// 鐒跺悗鎶婃湯灏惧€肩疆鎴愰閮ㄥ€?/span>
*__result = _GLIBCXX_MOVE(*__first);
// 姝ゆ椂鍫嗗ご宸茬粡涓嶅啀鏄渶灏忓€间簡锛岃€屾槸鏈€鍒濈殑鏈熬鍊硷紝鎵€浠ラ渶瑕佽皟鏁存暣涓爢
std::__adjust_heap(__first, _DistanceType(0),
_DistanceType(__last - __first),
_GLIBCXX_MOVE(__value), __comp);
}__adjust_heap()
鍑芥暟鏄釜浠€涔堜笢涓滐紵杩欎釜 __adjust_heap()
鍑芥暟灏辨槸鐢ㄦ潵璋冩暣鏁翠釜鍫嗙殑鍑芥暟锛屽畬鎴愪簡涓婃函鍜?span>涓嬫函鎿嶄綔锛屽涓嬶細template<typename _RandomAccessIterator, typename _Distance,
typename _Tp, typename _Compare>
void
__adjust_heap(_RandomAccessIterator __first, _Distance __holeIndex,
_Distance __len, _Tp __value, _Compare __comp) {
const _Distance __topIndex = __holeIndex; // 璁颁笅鏉ユ礊鑺傜偣鎵€鎸囧悜鐨勮妭鐐?/span>
_Distance __secondChild = __holeIndex; // 鐢ㄦ潵鎸囧悜娲炶妭鐐瑰彸杈圭殑鑺傜偣
while (__secondChild < (__len - 1) / 2) {
// 鎵惧嚭鏂版礊鑺傜偣鐨勫彸鑺傜偣
__secondChild = 2 * (__secondChild + 1);
// 姣旇緝涓€涓嬫礊鑺傜偣宸﹀彸涓よ竟鐨勫€硷紝鐒跺悗鐢?nbsp;__secondChild 鎸囧悜杈冨ぇ鐨勫€?/span>
if (__comp(__first + __secondChild, __first + (__secondChild - 1))) __secondChild--;
// 涓嬫函杩囩▼锛屾洿鏂版礊鑺傜偣鐨勫€硷紝浣夸箣鎴愪负杈冨ぇ鍊?/span>
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + __secondChild));
// 鐒跺悗鍐嶆妸娲炲彿涓嬬Щ鍒拌緝澶ц妭鐐瑰
__holeIndex = __secondChild;
}
// 濡傛灉娌℃湁鍙冲瓙鑺傜偣锛屽彧鏈夊乏瀛愯妭鐐?/span>
if ((__len & 1) == 0 && __secondChild == (__len - 2) / 2) {
__secondChild = 2 * (__secondChild + 1);
// 璁╁乏瀛愯妭鐐逛綅娲炲€硷紝鐒跺悗鍐嶈娲炲彿涓嬬Щ鍒板乏瀛愯妭鐐瑰
*(__first + __holeIndex) = _GLIBCXX_MOVE(*(__first + (__secondChild - 1)));
__holeIndex = __secondChild - 1;
}
// 瀵煎叆姣旇緝鍑芥暟涔嬬被鐨勬潅涓冩潅鍏?/span>
__decltype(__gnu_cxx::__ops::__iter_comp_val(_GLIBCXX_MOVE(__comp)))
__cmp(_GLIBCXX_MOVE(__comp));
// 鍐嶈繘琛屼竴娆′笂婧搷浣?/span>
std::__push_heap(__first, __holeIndex, __topIndex, _GLIBCXX_MOVE(__value), __cmp);
}int main() {
vector<int> iv = {0, 1, 2, 3, 4, 8, 9, 3, 5};
make_heap(iv.begin(), iv.end());
cout << iv << endl; // 9 5 8 3 4 0 2 3 1
pop_heap(iv.begin(), iv.end());
cout << iv.back() << endl; // 9
cout << iv << endl; // 8 7 4 3 5 0 2 3 1 銆?銆?/span>
}sort_heap
绠楁硶銆?/p>
1.3
sort_heap
绠楁硶pop_heap
绠楁硶涓嶉毦鐪嬪嚭锛屾瘡涓€娆$殑 pop_heap
鎿嶄綔閮戒細鎶婂綋鍓嶅爢鐨勬渶澶у€兼墧鍒?vector
瀹瑰櫒灏鹃儴鍘伙紝閭d箞鍙互杩欐牱鑰冭檻锛氭寔缁鏁翠釜 heap
杩涜 pop_heap
鎿嶄綔锛岄偅涔堟瘡娆″皢鎿嶄綔鑼冨洿浠庡悗鍚戝墠鍙互缂╁噺涓€涓厓绱狅紝褰撹杩囩▼鎵ц瀹屽悗锛屽氨鍙互寰楀埌涓€涓湁搴忓簭鍒楋紝渚嬪锛?/p>
int main() {
vector<int> iv = {0, 1, 2, 3, 4, 8, 9, 3, 5};
make_heap(iv.begin(), iv.end());
sort_heap(iv.begin(), iv.end());
cout << iv << endl; // 0 1 2 3 3 4 5 7 8 9
}template<typename _RandomAccessIterator, typename _Compare>
void
__sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare &__comp) {
while (__last - __first > 1) {
// 涓嶅仠鍦板幓 pop_heap 锛屽悓鏃惰鎶婃湯灏炬寚閽堜笉鏂墠绉?/span>
--__last;
std::__pop_heap(__first, __last, __last, __comp);
}
}
template<typename _RandomAccessIterator>
inline void
sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last) {
// 鍒犳帀浜嗕竴浜涘鐞嗚В鏁翠釜绋嬪簭鏃犲叧绱ц鐨勬鏌ョ▼搴?/span>
__gnu_cxx::__ops::_Iter_less_iter __comp;
std::__sort_heap(__first, __last, __comp);
}
template<typename _RandomAccessIterator, typename _Compare>
inline void
sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) {
// 鎴戝張鍒犳帀浜嗕竴浜涘鐞嗚В鏁翠釜绋嬪簭鏃犲叧绱ц鐨勬鏌ョ▼搴?/span>
typedef __decltype(__comp) _Cmp;
__gnu_cxx::__ops::_Iter_comp_iter<_Cmp> __cmp(_GLIBCXX_MOVE(__comp));
std::__sort_heap(__first, __last, __cmp);
}1.4
make_heap
绠楁硶heap
锛屽叾涓昏渚濇嵁灏辨槸瀹屽叏浜屽弶鏍戠殑闅愬紡琛ㄨ堪銆傚叿浣撶畻娉曞疄鐜扮粏鑺傚涓嬶細template<typename _RandomAccessIterator, typename _Compare>
void __make_heap(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare &__comp) {
typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
// 濡傛灉鏁翠釜搴忓垪澶у皬閮戒笉瓒?2锛岄偅鎺掑畠骞插暐锛燄煒?/span>
if (__last - __first < 2) return;
// 璁板綍涓嬫潵闇€瑕佹搷浣滅殑鑼冨洿
const _DistanceType __len = __last - __first;
// 鎵惧埌鏈熬鍏冪礌鐨勭埗鑺傜偣
_DistanceType __parent = (__len - 2) / 2;
while (true) {
// 閲嶆柊鎺掑竷浠?nbsp;parent 涓洪鐨勫瓙鏍戯紝
_ValueType __value = _GLIBCXX_MOVE(*(__first + __parent));
std::__adjust_heap(__first, __parent, __len, _GLIBCXX_MOVE(__value), __comp
);
// 濡傛灉璧板畬浜嗘牴鑺傜偣锛岄偅涔堝氨缁撴潫杩斿洖
if (__parent == 0) return;
// 宸茬粡鎷嶅ソ搴忕殑瀛愭爲澶村悜鍓嶇Щ鍔ㄤ竴涓寚閽?/span>
__parent--;
}
}2. 涓€浜涗緥瀛?/h1>
/**
* Created by Xiaozhong on 2020/11/21.
* Copyright (c) 2020/11/21 Xiaozhong. All rights reserved.
*/
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
ostream &operator<<(ostream &os, const vector<int> &v) {
for (auto i : v) os << i << " ";
return os;
}
int main() {
vector<int> iv = {0, 1, 2, 3, 4, 8, 9, 3, 5};
make_heap(iv.begin(), iv.end());
cout << iv << endl; // 9 5 8 3 4 0 2 3 1
iv.push_back(7);
push_heap(iv.begin(), iv.end());
cout << iv << endl; // 9 7 8 3 5 0 2 3 1 4
pop_heap(iv.begin(), iv.end());
cout << iv.back() << endl; // 9
cout << iv << endl; // 8 7 4 3 5 0 2 3 1 銆?銆?/span>
iv.pop_back();
sort_heap(iv.begin(), iv.end());
cout << iv << endl; // 0 1 2 3 3 4 5 7 8
}
以上是关于鎺㈢储STL锛氬爢鏁版嵁缁撴瀯鍙婄畻娉?/h1>