日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

C++/C++11中std::priority_queue的使用

發布時間:2023/11/27 生活经验 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++/C++11中std::priority_queue的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

std::priority_queue:在優先隊列中,優先級高的元素先出隊列,并非按照先進先出的要求,類似一個堆(heap)。其模板聲明帶有三個參數,priority_queue<Type, Container, Functional>, 其中Type為數據類型,Container為保存數據的容器,Functional為元素比較方式。Container必須是用數組實現的容器,比如 vector, deque. STL里面默認用的是vector. 比較方式默認用operator< , 所以如果把后面兩個參數缺省的話,優先隊列就是大頂堆,隊頭元素最大。

std::priority_queue: priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion. This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue). Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the "back" of the specific container, which is known as the top of the priority queue.

A priority queue is a queue data structure that has the particular property of being sorted by priority. You can decide what priority to give items depending on the data type.

下面是從其他文章中copy的測試代碼,詳細內容介紹可以參考對應的reference:

#include "priority_queue.hpp"
#include <iostream>
#include <queue> // std::priority_queue
#include <string>
#include <vector>
#include <functional> // std::greater
#include <deque>
#include <list>//template < class T,
//	   class Container = vector<T>,
//	   class Compare = less<typename Container::value_type> >
//class priority_queue;///
// reference: http://www.cplusplus.com/reference/queue/priority_queue/
int test_priority_queue_1()
{
{ // std::priority_queue::push: Inserts a new element in the priority_queue.// The content of this new element is initialized to val. inserts element and sorts the underlying container// std::priority_queue::pop: Removes the element on top of the priority_queue, removes the top element,// effectively reducing its size by one. The element removed is the one with the highest value.// std::priority_queue::empty: Test whether container is emptystd::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';
}{ // std::priority_queue::emplace: C++11, Adds a new element to the priority_queue.// This new element is constructed in place passing args as the arguments for its constructor.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';
}{// std::priority_queue::size: Returns the number of elements in the priority_queuestd::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';
}{ // std::priority_queue::top: Returns a constant reference to the top element in the priority_queue.// The top element is the element that compares higher in the priority_queue,// and the next that is removed from the container when priority_queue::pop is called.std::priority_queue<int> mypq;mypq.push(10);mypq.push(20);mypq.push(15);std::cout << "mypq.top() is now " << mypq.top() << '\n';
}{ // std::priority_queue::swap: C++11, Exchanges the contents of the container adaptor by those of x,// swapping both the underlying container value and their comparison function using// the corresponding swap non-member functions (unqualified)// std::swap (priority_queue): Exchange contents of priority queuesstd::priority_queue<int> foo, bar;foo.push(15); foo.push(30); foo.push(10);bar.push(101); bar.push(202);foo.swap(bar);//std::swap(foo, bar);std::cout << "size of foo: " << foo.size() << '\n';std::cout << "size of bar: " << bar.size() << '\n';
}return 0;
}//
// reference: http://en.cppreference.com/w/cpp/container/priority_queue
template<typename T>
static void print_queue(T& q) {while (!q.empty()) {std::cout << q.top() << " ";q.pop();}std::cout << '\n';
}int test_priority_queue_2()
{std::priority_queue<int> q;for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})q.push(n);print_queue(q);std::priority_queue<int, std::vector<int>, std::greater<int> > q2;for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})q2.push(n);print_queue(q2);// Using lambda to compare elements.auto cmp = [](int left, int right) { return (left ^ 1) < (right ^ 1); };std::priority_queue<int, std::vector<int>, decltype(cmp)> q3(cmp);for (int n : {1, 8, 5, 6, 3, 4, 0, 9, 7, 2})q3.push(n);print_queue(q3);return 0;
}/
// reference: http://www.technical-recipes.com/2011/priority-queues-and-min-priority-queues-in-c/
struct compare {bool operator()(const int& l, const int& r) {return l > r;}
};int test_priority_queue_3()
{using namespace std;priority_queue<int, vector<int>, compare > pq;pq.push(3);pq.push(5);pq.push(1);pq.push(8);std::cout << "pq contains:";while (!pq.empty()) {std::cout << ' ' << pq.top();pq.pop();}std::cout << '\n';return 0;
}/
// reference: https://msdn.microsoft.com/en-us/library/4ef4dae9.aspx
int test_priority_queue_4()
{
{ // priority_queue::container_type: A type that provides the base container to be adapted.// priority_queue::empty: Tests if a priority_queue is empty.// true if the priority_queue is empty; false if the priority_queue is nonempty.using namespace std;// Declares priority_queues with default deque base containerpriority_queue <int> q1, s2;q1.push(1);if (q1.empty()) cout << "The priority_queue q1 is empty." << endl;else cout << "The priority_queue q1 is not empty." << endl;if (s2.empty()) cout << "The priority_queue s2 is empty." << endl;else cout << "The priority_queue s2 is not empty." << endl;
}{ // priority_queue::pop: Removes the largest element of the priority_queue from the top position.using namespace std;priority_queue <int> q1, s2;q1.push(10);q1.push(5);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 " << ii << "." << 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;
}{ // priority_queue::priority_queue: Constructs a priority_queue that is empty or// that is a copy of a range of a base container object or of another priority_queueusing 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);q1.push(5);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 v5  priority_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;
}{ // priority_queue::push: Adds an element to the priority queue based on the priority of the element from operator<.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;
}{ // priority_queue::size: Returns the number of elements in the priority_queue.// priority_queue::size_type: An unsigned integer type that can represent the number of elements in a priority_queue.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;
}{ // priority_queue::top: Returns a const reference to the largest element at the top of the priority_queue.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;
}{ // priority_queue::value_type: A type that represents the type of object stored as an element in a priority_queue.using namespace std;// Declares priority_queues with default deque base containerpriority_queue<int>::value_type AnInt;AnInt = 69;cout << "The value_type is AnInt = " << AnInt << endl;priority_queue<int> q1;q1.push(AnInt);cout << "The element at the top of the priority_queue is " << q1.top() << "." << endl;
}return 0;
}

GitHub: https://github.com/fengbingchun/Messy_Test

總結

以上是生活随笔為你收集整理的C++/C++11中std::priority_queue的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 精品国产一区二区三 | 538国产视频 | 91社区在线播放 | 久久久久香蕉视频 | 在线伊人 | 糖心logo在线观看 | 国产成人无码精品久在线观看 | 久久久久久久女国产乱让韩 | 国产第一页在线播放 | 激情小说五月天 | 欧美bbw视频 | 精品综合久久 | 桃色一区二区三区 | 狠狠艹视频 | 亚洲美女精品 | heyzo朝桐光一区二区 | 欧美18aaaⅹxx | 亚洲av永久一区二区三区蜜桃 | 成人精品网址 | 一本久道视频一本久道 | 性生生活大片又黄又 | 韩国成人在线 | 国语对白做受按摩的注意事项 | 三级第一页 | 久久国产精品一区 | 色就是色av | 在线观看波多野结衣 | 人人舔人人插 | 亚洲天堂影院在线观看 | 免费特级黄色片 | 欧美日韩免费在线视频 | 影音先锋中文字幕资源 | 国产黄色片免费看 | 日本高清视频免费看 | 亚洲一区二区黄片 | 久久久久久成人 | 欧美天天影院 | 国产精品探花一区二区三区 | 大色av | 国产精品视频亚洲 | 99福利网 | 男插女视频网站 | 欧美aaaaaaaaaa | 性一交一乱一区二区洋洋av | 国产av精国产传媒 | 老鸭窝成人 | 最新av免费在线观看 | 91亚洲精品久久久久久久久久久久 | 波多野结衣在线影院 | 青青草视频在线观看 | 美女扒开屁股让男人桶 | 国产欧美视频一区 | 每日更新av | 亚洲精品~无码抽插 | 97狠狠干 | 日日骚av| 91麻豆产精品久久久久久 | 男男gay做受xx| xxxxx在线观看 | 黄视频免费在线观看 | 久久久视频在线观看 | 男人和女人日批视频 | 日日噜噜噜夜夜爽爽狠狠视频97 | 欧美激情一区 | 日韩第一视频 | 亚洲日本视频在线观看 | 午夜影视av | 久久久精品动漫 | 国产在线一级片 | 一级片国产 | 辟里啪啦国语版免费观看 | 波多野结衣99| 美女扒开尿口给男人看 | 免费看毛片的网站 | 在线h网 | 免费日韩 | 韩国精品一区二区三区 | 欧美图片一区二区 | 亚洲一区二区福利 | 久久久久久久久福利 | 天天色天天操天天 | 四川话毛片少妇免费看 | аⅴ资源天堂资源库在线 | 日韩影院一区二区 | 国产精品爱啪在线线免费观看 | 韩国伦理av| 强公把我次次高潮hd | 午夜在线一区二区三区 | 亚洲人女屁股眼交6 | 播播网色播播 | 超碰97人 | 亚洲一区二区三区在线视频 | 欧美小视频在线 | 国产稀缺精品盗摄盗拍 | 激情综合五月 | 国产网红女主播精品视频 | 手机在线观看av片 | 福利视频亚洲 | 久久国语精品 |