<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ### 前言 ? ? ? priority_queue是擁有優先級的queue,不過它容器內的元素并不是根據加入順序排列,而是根據用戶定義的優先級進行排列。priority_queue只能在隊列尾部加入元素,在頭部取出元素。不能遍歷容器,因此不需要自己設置迭代器。在SGI STL的源碼<stl_queue.h>的class?priority_queue設計中,它是基于某種容器作為底部結構的,默認容器是vector容器,用戶也可以自己指定容器的類型。 ### priority_queue容器配接器 ? 下面給出源碼剖析: ~~~ template <class _Tp, class _Sequence __STL_DEPENDENT_DEFAULT_TMPL(vector<_Tp>), class _Compare __STL_DEPENDENT_DEFAULT_TMPL(less<typename _Sequence::value_type>) > class priority_queue { // requirements: __STL_CLASS_REQUIRES(_Tp, _Assignable); __STL_CLASS_REQUIRES(_Sequence, _Sequence); __STL_CLASS_REQUIRES(_Sequence, _RandomAccessContainer); typedef typename _Sequence::value_type _Sequence_value_type; __STL_CLASS_REQUIRES_SAME_TYPE(_Tp, _Sequence_value_type); __STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Tp, _Tp); public: // priority_queue僅支持對頭部和尾部的操作, 所以不定義STL要求的 // pointer, iterator, difference_type typedef typename _Sequence::value_type value_type; typedef typename _Sequence::size_type size_type; typedef _Sequence container_type; typedef typename _Sequence::reference reference; typedef typename _Sequence::const_reference const_reference; protected: _Sequence c;//底層容器,默認為vector,用戶可自行指定容器類型 _Compare comp;//優先級決策方式 public: //******************************* //* 構造函數 //* priority_queue() //* explicit priority_queue(const Compare& __x) //* explicit priority_queue (const Compare& comp = Compare(), //* const Container& ctnr = Container()); //* template <class InputIterator> //* priority_queue (InputIterator first, InputIterator last, //* const Compare& comp = Compare(), //* const Container& ctnr = Container()); //******************************* priority_queue() : c() {} explicit priority_queue(const _Compare& __x) : c(), comp(__x) {} priority_queue(const _Compare& __x, const _Sequence& __s) : c(__s), comp(__x) { make_heap(c.begin(), c.end(), comp); } #ifdef __STL_MEMBER_TEMPLATES template <class _InputIterator> priority_queue(_InputIterator __first, _InputIterator __last) : c(__first, __last) { make_heap(c.begin(), c.end(), comp); } template <class _InputIterator> priority_queue(_InputIterator __first, _InputIterator __last, const _Compare& __x) : c(__first, __last), comp(__x) { make_heap(c.begin(), c.end(), comp); } template <class _InputIterator> priority_queue(_InputIterator __first, _InputIterator __last, const _Compare& __x, const _Sequence& __s) : c(__s), comp(__x) { c.insert(c.end(), __first, __last); make_heap(c.begin(), c.end(), comp); } #else /* __STL_MEMBER_TEMPLATES */ priority_queue(const value_type* __first, const value_type* __last) : c(__first, __last) { make_heap(c.begin(), c.end(), comp); } priority_queue(const value_type* __first, const value_type* __last, const _Compare& __x) : c(__first, __last), comp(__x) { make_heap(c.begin(), c.end(), comp); } priority_queue(const value_type* __first, const value_type* __last, const _Compare& __x, const _Sequence& __c) : c(__c), comp(__x) { c.insert(c.end(), __first, __last); make_heap(c.begin(), c.end(), comp); } #endif /* __STL_MEMBER_TEMPLATES */ //判斷容器是否為空 bool empty() const { return c.empty(); } //返回容器元素的個數 size_type size() const { return c.size(); } //返回優先級最高元素的引用 const_reference top() const { return c.front(); } //新增元素,并根據優先級調整堆 void push(const value_type& __x) { __STL_TRY { c.push_back(__x); push_heap(c.begin(), c.end(), comp); } __STL_UNWIND(c.clear()); } //彈出優先級最高的元素 void pop() { __STL_TRY { pop_heap(c.begin(), c.end(), comp); c.pop_back(); } __STL_UNWIND(c.clear()); } }; // no equality is provided __STL_END_NAMESPACE #endif /* __SGI_STL_INTERNAL_QUEUE_H */ // Local Variables: // mode:C++ // End: ~~~ 舉例說明該容器的使用: ~~~ // constructing priority queues #include <iostream> // std::cout #include <queue> // std::priority_queue #include <vector> // std::vector #include <functional> // std::greater class mycomparison { bool reverse; public: mycomparison(const bool& revparam=false) {reverse=revparam;} bool operator() (const int& lhs, const int&rhs) const { if (reverse) return (lhs>rhs); else return (lhs<rhs); } }; int main () { int myints[]= {10,60,50,20}; std::priority_queue<int> first; std::priority_queue<int> second (myints,myints+4); std::priority_queue<int, std::vector<int>, std::greater<int> > third (myints,myints+4); std::cout << "third = ( "; while ( !third.empty( ) ) { std::cout << third.top( ) << " "; third.pop( ); } std::cout << ")" << std::endl; // using mycomparison: typedef std::priority_queue<int,std::vector<int>,mycomparison> mypq_type; mypq_type fourth (myints,myints+4); // less-than comparison mypq_type fifth (mycomparison(true)); // greater-than comparison std::cout << "fourth = ( "; while ( !fourth.empty( ) ) { std::cout << fourth.top( ) << " "; fourth.pop( ); } std::cout << ")" << std::endl; std::cout << "fifth = ( "; while ( !fifth.empty( ) ) { std::cout << fifth.top( ) << " "; fifth.pop( ); } std::cout << ")" << std::endl; for (int i = 0; i < 5; i++) { fifth.push(i*10); } std::cout <<"after push the elements,fifth size is :"<<fifth.size()<<std::endl; std::cout << "after push the elements,fifth = ( "; while ( !fifth.empty( ) ) { std::cout << fifth.top( ) << " "; fifth.pop( ); } std::cout << ")" << std::endl; system("pause"); return 0; } Output: third = ( 10 20 50 60 ) fourth = ( 60 50 20 10 ) fifth = ( ) after push the elements,fifth size is :5 after push the elements,fifth = ( 0 10 20 30 40 ) ~~~ 參考資料: 《STL源碼剖析》侯捷
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看