C++——std::Priority_queue

寫在前面
這一部分學習一下優先級隊列這樣一種STL容器。priority_ queue 優先級隊列是一個擁有權值概念的單向隊列queue,在這個隊列中,全部元素是按優先級排列的(也能夠認爲queue是個按進入隊列的前後作爲優先級的優先級隊列——先進入隊列的元素優先權要高於後進入隊列的元素)。在計算機操做系統中,優先級隊列的使用是至關頻繁的,進線程調度都會用到。在STL的具體實現中,priority_queue也是以別的容器做爲底部結構,再根據堆的處理規則來調整元素之間的位置。ios

頭文件包含
要在程序中使用priority_queue須要包含queue的頭文件。算法

#include <queue>  
1
priority_ queue::priority_queue
構造一個優先級隊列,能夠直接構造一個空隊列或者是現有某基礎容器對象的拷貝。參考:priority_queue Class——MSDN,其構造方法一共有七種:app

priority_queue();less

explicit priority_queue(const Traits&_comp);函數

priority_queue(const Traits&_comp, const container_type& _Cont);學習

priority_queue(const priority_queue& right);spa

template <class InputIterator>  
priority_queue(InputIterator first, InputIterator last);操作系統

template <class InputIterator>  
priority_queue(InputIterator first, InputIterator last, const Traits&_comp);.net

template <class InputIterator>  
priority_queue(InputIterator first, InputIterator last, const Traits&_comp, const container_type& _Cont);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
其中,各參數含義以下:線程

_ comp:是constTraits的比較函數,用來對隊列中的元素進行排列。默認爲基本容器的默認排序函數(由小到大);
_Cont:經過拷貝容器的方式創造隊列的那個基本容器。
right:經過拷貝另外一個隊列的方式創造一個優先級隊列的那個隊列。
first:經過拷貝某一個容器來建立一個優先級隊列的容器中的第一個元素。
last:經過拷貝某一個容器來建立一個優先級隊列的容器中的最後一個元素。
具體實例代碼可參考:

   // The first member function declares priority_queue  
   // with a default vector base container  
   priority_queue <int> q1;

   // Explicitly declares a priority_queue with nondefault  
   // deque base container  
   priority_queue <int, deque <int> > q2;  

   // The third member function declares a priority_queue   
   // with a vector base container and specifies that the comparison   
   // function greater is to be used for ordering elements  
   priority_queue <int, vector<int>, std::greater<int> > q3;  

   // The fourth member function declares a priority_queue and  
   // initializes it with elements copied from another container:  
   // first, inserting elements into q1, then copying q1 elements into q4  
   q1.push( 100 );  
   q1.push( 200 );  
   priority_queue <int> q4( q1 );  

   // The fifth member function declares and  
   // initializes a priority_queue q5 by copying the  
   // range v5[ first,  last) from vector<int> v5 {10,20,30}
   priority_queue <int> q5( v5.begin( ), v5.begin( ) + 2 );  

   // The sixth member function declares a priority_queue q6  
   // with a comparison function greater and initializes q6  
   // by copying the range v5[ first,  last) from vector v5  
   priority_queue <int, vector<int>, std::greater<int> > q6( v5.begin( ), v5.begin( ) + 2 );  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Member functions


1.empty() 
判斷優先級隊列是否爲空。示例代碼以下:

// priority_queue::empty
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue

int main ()
{
  std::priority_queue<int> mypq;
  int sum (0);

  for (int i=1;i<=10;i++) mypq.push(i);

  while (!mypq.empty())
  {
     sum += mypq.top();
     mypq.pop();
  }

  std::cout << "total: " << sum << '\n';

  return 0;
}

//Output:
//total: 55
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2.size() 
返回優先級隊列中元素的個數。示例代碼以下:

// priority_queue::size
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue

int main ()
{
  std::priority_queue<int> myints;
  std::cout << "0. size: " << myints.size() << '\n';

  for (int i=0; i<5; i++) myints.push(i);
  std::cout << "1. size: " << myints.size() << '\n';

  myints.pop();
  std::cout << "2. size: " << myints.size() << '\n';

  return 0;
}

//Output:

//0. size: 0
//1. size: 5
//2. size: 4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
3.top() 
獲取隊列頂層元素。示例代碼以下:

// priority_queue::top
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue

int main ()
{
  std::priority_queue<int> mypq;

  mypq.push(10);
  mypq.push(20);
  mypq.push(15);

  std::cout << "mypq.top() is now " << mypq.top() << '\n';

  return 0;
}

//Output:
//mypq.top() is now 20
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
4.push() 
向隊列中插入元素。示例代碼以下:

// priority_queue::push/pop
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue

int main ()
{
  std::priority_queue<int> mypq;

  mypq.push(30);
  mypq.push(100);
  mypq.push(25);
  mypq.push(40);

  std::cout << "Popping out elements...";
  while (!mypq.empty())
  {
     std::cout << ' ' << mypq.top();
     mypq.pop();
  }
  std::cout << '\n';

  return 0;
}

//Output:
//Popping out elements... 100 40 30 25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
5.pop() 
刪除隊列頂部元素。示例代碼以下:

// priority_queue::push/pop
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue

int main ()
{
  std::priority_queue<int> mypq;

  mypq.push(30);
  mypq.push(100);
  mypq.push(25);
  mypq.push(40);

  std::cout << "Popping out elements...";
  while (!mypq.empty())
  {
     std::cout << ' ' << mypq.top();
     mypq.pop();
  }
  std::cout << '\n';

  return 0;
}

//Output:
//Popping out elements... 100 40 30 25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
6.emplace() 
構造並向隊列中插入一個元素。示例代碼以下:

// priority_queue::emplace
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue
#include <string>         // std::string

int main ()
{
  std::priority_queue<std::string> mypq;

  mypq.emplace("orange");
  mypq.emplace("strawberry");
  mypq.emplace("apple");
  mypq.emplace("pear");

  std::cout << "mypq contains:";
  while (!mypq.empty())
  {
     std::cout << ' ' << mypq.top();
     mypq.pop();
  }
  std::cout << '\n';

  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
7.swap() 
交換兩個隊列的內容。示例代碼以下:

// priority_queue::swap
#include <iostream>       // std::cout
#include <queue>          // std::priority_queue

int main ()
{
  std::priority_queue<int> foo,bar;
  foo.push (15); foo.push(30); foo.push(10);
  bar.push (101); bar.push(202);

  foo.swap(bar);

  std::cout << "size of foo: " << foo.size() << '\n';
  std::cout << "size of bar: " << bar.size() << '\n';

  return 0;
}

//Output:

//size of foo: 2
//size of bar: 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
VS2008Priority_queue源代碼
參考博客:STL系列之五 priority_queue 優先級隊列,VS2008中priority_queue源代碼以下:

//VS2008中 priority_queue的定義 MoreWindows整理( http://blog.csdn.net/MoreWindows )  
template<class _Ty, class _Container = vector<_Ty>, class _Pr = less<typename _Container::value_type> > //默認以vector爲容器的  
class priority_queue  
{   // priority queue implemented with a _Container  
public:  
    typedef _Container container_type;  
    typedef typename _Container::value_type value_type;  
    typedef typename _Container::size_type size_type;  
    typedef typename _Container::reference reference;  
    typedef typename _Container::const_reference const_reference;  

    priority_queue() : c(), comp()  
    {   // construct with empty container, default comparator  
    }  

    explicit priority_queue(const _Pr& _Pred) : c(), comp(_Pred)  
    {   // construct with empty container, specified comparator  
    }  

    priority_queue(const _Pr& _Pred, const _Container& _Cont) : c(_Cont), comp(_Pred)  
    {   // construct by copying specified container, comparator  
        make_heap(c.begin(), c.end(), comp); //參見《STL系列之四 heap 堆的相關函數》  
    }  

    template<class _Iter>  
    priority_queue(_Iter _First, _Iter _Last) : c(_First, _Last), comp()  
    {   // construct by copying [_First, _Last), default comparator  
        make_heap(c.begin(), c.end(), comp);  
    }  

    template<class _Iter>  
    priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred) : c(_First, _Last), comp(_Pred)  
    {   // construct by copying [_First, _Last), specified comparator  
        make_heap(c.begin(), c.end(), comp);  
    }  

    template<class _Iter>  
    priority_queue(_Iter _First, _Iter _Last, const _Pr& _Pred, const _Container& _Cont) : c(_Cont), comp(_Pred)  
    {   // construct by copying [_First, _Last), container, and comparator  
        c.insert(c.end(), _First, _Last);  
        make_heap(c.begin(), c.end(), comp);  
    }  

    bool empty() const  
    {   // test if queue is empty  
        return (c.empty());  
    }  

    size_type size() const  
    {   // return length of queue  
        return (c.size());  
    }  

    const_reference top() const  
    {   // return highest-priority element  
        return (c.front());  
    }  

    reference top()  
    {   // return mutable highest-priority element (retained)  
        return (c.front());  
    }  

    void push(const value_type& _Pred)  
    {   // insert value in priority order  
        c.push_back(_Pred);  
        push_heap(c.begin(), c.end(), comp);  
    }  

    void pop()  
    {   // erase highest-priority element  
        pop_heap(c.begin(), c.end(), comp);  
        c.pop_back();  
    }  

protected:  
    _Container c;   // the underlying container  
    _Pr comp;   // the comparator functor  
};  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
此外,參考博客:priority_queue的用法,能夠經過堆算法實現相似功能,VS2008中源代碼可看作是這個的升級版。代碼以下:

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

class priority_queue
{
    private:
        vector<int> data;

    public:
        void push( int t ){ 
            data.push_back(t); 
            push_heap( data.begin(), data.end()); 
        }

        void pop(){
            pop_heap( data.begin(), data.end() );
            data.pop_back();
        }

        int top() { return data.front(); }         int size() { return data.size(); }         bool empty() { return data.empty(); } }; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 寫在後面 還有queue、stack、heap的用法沒有整理 ---------------------  做者:zy2317878  來源:CSDN  原文:https://blog.csdn.net/zy2317878/article/details/80597318  版權聲明:本文爲博主原創文章,轉載請附上博文連接!

相關文章
相關標籤/搜索