c++ priority_queues

來源:互聯網
上載者:User
priority_queue 優先順序隊列是一個擁有權值概念的單向隊列queue,在這個隊列中,所有元素是按優先順序排列的(也可以認為queue是個按進入隊列的先後做為優先順序的優先順序隊列——先進入隊列的元素優先權要高於後進入隊列的元素)。在電腦作業系統中,優先順序隊列的使用是相當頻繁的,進線程調度都會用到。在STL的具體實現中,priority_queue也是以別的容器作為底部結構,再根據堆的處理規則來調整元素之間的位置empty判斷是否為空白size返回元素的數量top返回位於頂部的引用的元素push將一個元素添加到頂部pop刪除頂部的元素


#include <queue>#include <vector>#include <deque>#include <list>#include <xfunctional>#include <iostream>//建構函式void priority_queueConstructor(void);//從priority_queue中刪除頂部的元素void priority_queue_pop(void);//將一個元素添加到priority_queue的頂部 void priority_queue_push(void);//返回priority_queue元素的數量 void priority_queue_size(void);//返回位於priority_queue頂部的引用的元素 void priority_queue_top(void);int main(){//priority_queueConstructor();//priority_queue_pop();//priority_queue_push();//priority_queue_size();//priority_queue_top();using namespace std;priority_queue <int> q1;q1.push(1);q1.push(2);q1.push(3);q1.push(4);q1.push(5);return 0;}//建構函式void priority_queueConstructor(void){using namespace std;// The first member function declares priority_queue// with a default vector base containerpriority_queue <int> q1;cout << "q1 = ( ";while (!q1.empty()){cout << q1.top() << " ";q1.pop();}cout << ")" << endl;// Explicitly declares a priority_queue with nondefault// deque base containerpriority_queue <int, deque <int> > q2;q2.push(5);q2.push(15);q2.push(10);cout << "q2 = ( ";while (!q2.empty()){cout << q2.top() << " ";q2.pop();}cout << ")" << endl;// This method of printing out the elements of a priority_queue// removes the elements from the priority queue, leaving it emptycout << "After printing, q2 has " << q2.size() << " elements." << endl;// 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 elementspriority_queue <int, vector<int>, greater<int> > q3;q3.push(2);q3.push(1);q3.push(3);cout << "q3 = ( ";while (!q3.empty()){cout << q3.top() << " ";q3.pop();}cout << ")" << endl;// 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 q4q1.push(100);q1.push(200);priority_queue <int> q4(q1);cout << "q4 = ( ";while (!q4.empty()){cout << q4.top() << " ";q4.pop();}cout << ")" << endl;// Creates an auxiliary vector object v5 to be used to initialize q5vector <int> v5;vector <int>::iterator v5_Iter;v5.push_back(10);v5.push_back(30);v5.push_back(20);cout << "v5 = ( ";for (v5_Iter = v5.begin(); v5_Iter != v5.end(); v5_Iter++)cout << *v5_Iter << " ";cout << ")" << endl;// The fifth member function declares and// initializes a priority_queue q5 by copying the// range v5[_First, _Last) from vector v5priority_queue <int> q5(v5.begin(), v5.begin() + 2);cout << "q5 = ( ";while (!q5.empty()){cout << q5.top() << " ";q5.pop();}cout << ")" << endl;// 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 v5priority_queue <int, vector<int>, greater<int> >q6(v5.begin(), v5.begin() + 2);cout << "q6 = ( ";while (!q6.empty()){cout << q6.top() << " ";q6.pop();}cout << ")" << endl;return;/*q1 = ( )q2 = ( 15 10 5 )After printing, q2 has 0 elements.q3 = ( 1 2 3 )q4 = ( 200 100 )v5 = ( 10 30 20 )q5 = ( 30 10 )q6 = ( 10 30 )請按任意鍵繼續. . .*/}//從priority_queue中刪除頂部的元素void priority_queue_pop(void){using namespace std;priority_queue <int> q1, s2;q1.push(10);q1.push(20);q1.push(30);priority_queue <int>::size_type i, iii;i = q1.size();cout << "The priority_queue length is " << i << "." << endl;const int& ii = q1.top();cout << "The element at the top of the priority_queue is "<< i << "." << endl;q1.pop();iii = q1.size();cout << "After a pop, the priority_queue length is "<< iii << "." << endl;const int& iv = q1.top();cout << "After a pop, the element at the top of the "<< "priority_queue is " << iv << "." << endl;return;/*The priority_queue length is 3.The element at the top of the priority_queue is 3.After a pop, the priority_queue length is 2.After a pop, the element at the top of the priority_queue is 20.請按任意鍵繼續. . .*/}//將一個元素添加到priority_queue的頂部 void priority_queue_push(void){using namespace std;priority_queue<int> q1;q1.push(10);q1.push(30);q1.push(20);priority_queue<int>::size_type i;i = q1.size();cout << "The priority_queue length is " << i << "." << endl;const int& ii = q1.top();cout << "The element at the top of the priority_queue is "<< ii << "." << endl;return;/*The priority_queue length is 3.The element at the top of the priority_queue is 30.請按任意鍵繼續. . .*/}//返回priority_queue元素的數量 void priority_queue_size(void){using namespace std;priority_queue <int> q1, q2;priority_queue <int>::size_type i;q1.push(1);i = q1.size();cout << "The priority_queue length is " << i << "." << endl;q1.push(2);i = q1.size();cout << "The priority_queue length is now " << i << "." << endl;return;/*The priority_queue length is 1.The priority_queue length is now 2.請按任意鍵繼續. . .*/}//返回位於priority_queue頂部的引用的元素 void priority_queue_top(void){using namespace std;priority_queue<int> q1;q1.push(10);q1.push(30);q1.push(20);priority_queue<int>::size_type i;i = q1.size();cout << "The priority_queue length is " << i << "." << endl;const int& ii = q1.top();cout << "The element at the top of the priority_queue is "<< ii << "." << endl;return;/*The priority_queue length is 3.The element at the top of the priority_queue is 30.請按任意鍵繼續. . .*/}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.