优先级队列

https://www.cnblogs.com/cielosun/p/5654595

htmlhttps://zhuanlan.zhihu.com/p/52807426

 

C++优先队列使用方法

王某某

王某某

CS

1 人赞同了该文章

定义:

  1. 优先队列是关于集合S的一种数据结构,集合中的每个元素都有key
  2. 最大优先队列支持以下操作:
  • Insert(S, x)
  • Max(S)
  • Extract-Max(S)
  • Increase-Key(S, x, k) 将集合S中元素x的key值增加到k,注意这里的k应该大于x原有的健值

二叉堆

对于新插入的元素,调整它的位置,保持堆的性质,即父节点的值比子节点要大

从倒数第二层,拥有子节点的节点开始,逐个进行MAX-HEAPIFY的操作

取出最大元素的时候,需要把集合中的最小元素拿到树顶,然后让它向下更新

注意,改变元素key值的时候,只能让key值变大,从而向上更新,因为堆的父节点比子节点大的属性限制导致向下更新方向的不确定性

 

 

 


C++99 中Priority_queue的用法

头文件

#include<queue>

 

定义

priority_queue<Type, Container, Functional>

 

Type为数据类型

Container为保存数据的容器

Functioanl为元素比较方式

后两项参数可以省略,默认为vector,比较方式为operator <

最简单的最大堆的定义可以如下

#include<queue>
priority_queue<int> p;

如果想做最简单的最小堆可以定义如下

#include<queue>
#include<functional> //std::greater
using namespace std;
priority_queue<int, vector<int>, greater<int>>

自定义优先级,利用最简单的结构体,重载 < 号

#include<iostream>
#include<queue>
#include<cstdlib>
using namespace std;
struct node{
  int x, y;
  node(int a, int b):
  x(a), y(b)
  {}
};

struct cmp{
  bool operator() (node a, node b){
    if (a.x==b.x) return a.y < b.y;
    else return a.x < b.x;
  }
};

priority_queue<node, vector<node>, cmp>;

 

此外,下面是C++99 官方网站的构造格式以及各类函数

#include<iostream>
#include<queue>
#include<vector>
#include<functional>

using namespace std;
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};

	priority_queue<int> first;
	priority_queue<int> second (myints, myints+4);
	priority_queue<int, vector<int>, greater<int>> third(myints, myints+4);

	typedef priority_queue<int, vector<int>, mycomparison> mypq_type;
	mypq_type fourth;
	mypq_type fifth (mycomparison(true));
}

同时还有下列函数

  • empty
  • size
  • top
  • pop
  • push
  • emplace
  • swap

发布于 2018-12-19

算法与数据结构

​赞同 1​​添加评论

​分享

​收藏

推荐阅读

C++虚函数

C++虚函数

我是小将发表于Moder...

回溯法

回溯法

法号桑菜

C++右值引用

C++右值引用

我是小将

C++ 学习笔记之——STL 库 queue

 

CieloSun

我的人生就像现在这样享清福就够了。

浅谈C++ STL中的优先队列(priority_queue)

从我以前的博文能看出来,我是一个队列爱好者,很多并不是一定需要用队列实现的算法我也会采用队列实现,主要是由于队列和人的直觉思维的一致性导致的。

今天讲一讲优先队列(priority_queue),实际上,它的本质就是一个heap,我从STL中扒出了它的实现代码,大家可以参考一下。

首先函数在头文件<queue>中,归属于命名空间std,使用的时候需要注意。

队列有两种常用的声明方式:

std::priority_queue<T> pq;
std::priority_queue<T, std::vector<T>, cmp> pq;

 

第一种实现方式较为常用,接下来我给出STL中的对应声明,再加以解释。

template<class _Ty,
    class _Container = vector<_Ty>,
    class _Pr = less<typename _Container::value_type> >
    class priority_queue

 

大家可以看到,默认模板有三个参数,第一个是优先队列处理的类,第二个参数比较有特点,是容纳优先队列的容器。实际上,优先队列是由这个容器+C语言中关于heap的相关操作实现的。这个容器默认是vector,也可以是dequeue,因为后者功能更强大,而性能相对于vector较差,考虑到包装在优先队列后,后者功能并不能很好发挥,所以一般选择vector来做这个容器。第三个参数比较重要,支持一个比较结构,默认是less,默认情况下,会选择第一个参数决定的类的<运算符来做这个比较函数。

接下来开始坑爹了,虽然用的是less结构,然而,队列的出队顺序却是greater的先出!就是说,这里这个参数其实很傲娇,表示的意思是如果!cmp,则先出列,不管这样实现的目的是啥,大家只能接受这个实现。实际上,这里的第三个参数可以更换成greater,像下面这样:

std::priority_queue<T, std::vector<T>, greater<T>> pq;

 

一般大家如果是自定义类就干脆重载<号时注意下方向了,没人在这里麻烦,这个选择基本上是在使用int类还想小值先出列时。

从上面的剖析我们也就知道了,想要让自定义类能够使用优先队列,我们要重载小于号。

复制代码

class Student
{
    int id;
    char name[20];
    bool gender;
    bool operator < (Student &a) const
    {
        return id > a.id;
    }
};

复制代码

 

就拿这个例子说,我们想让id小的先出列,怎么办,就要很违和的给这个小于符号重载成实际上是大于的定义。

如果我们不使用自定义类,又要用非默认方法去排序怎么办?就比如说在Dijkstra中,我们当然不会用点的序号去排列,无论是正序还是反序,我们想用点到起点的距离这个值来进行排序,我们怎样做呢?细心的读者在阅读我的有关Dijkstra那篇文章时应该就发现了做法——自定义比较结构。优先队列默认使用的是小于结构,而上文的做法是为我们的自定义类去定义新的小于结构来符合优先队列,我们当然也可以自定义比较结构。自定义方法以及使用如下,我直接用Dijkstra那篇的代码来说明:

复制代码

int cost[MAX_V][MAX_V];
int d[MAX_V], V, s;
//自定义优先队列less比较函数
struct cmp
{
    bool operator()(int &a, int &b) const
    {
        //因为优先出列判定为!cmp,所以反向定义实现最小值优先
        return d[a] > d[b];
    }
};
void Dijkstra()
{
    std::priority_queue<int, std::vector<int>, cmp> pq;
    pq.push(s);
    d[s] = 0;
    while (!pq.empty())
    {
        int tmp = pq.top();pq.pop();
        for (int i = 0;i < V;++i)
        {
            if (d[i] > d[tmp] + cost[tmp][i])
            {
                d[i] = d[tmp] + cost[tmp][i];
                pq.push(i);
            }
        }
    }
}

复制代码

 

优先队列的日常使用,了解上面那些就已经足够。下面给出优先队列的所有成员函数的STL实现方法,希望你看完没有一脸卧槽的感觉。c就是你声明时候的那个vector或者其他容器。

复制代码

    void push(value_type&& _Val)
        {    // insert element at beginning
        c.push_back(_STD move(_Val));
        push_heap(c.begin(), c.end(), comp);
        }

    template<class... _Valty>
        void emplace(_Valty&&... _Val)
        {    // insert element at beginning
        c.emplace_back(_STD forward<_Valty>(_Val)...);
        push_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());
        }

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

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

复制代码

 

http://c.biancheng.net/view/480.html

阅读:1,273

C++ priority_queue(STL priority_queue)用法详解

C++ queueC++堆 >

C语言中文网推出辅导班啦,包括「C语言辅导班、C++辅导班、算法/数据结构辅导班」,全部都是一对一教学:一对一辅导 + 一对一答疑 + 布置作业 + 项目实践 + 永久学习。QQ在线,随时响应!

不出所料,priority_queue 容器适配器定义了一个元素有序排列的队列。默认队列头部的元素优先级最高。因为它是一个队列,所以只能访问第一个元素,这也意味着优先级最高的元素总是第一个被处理。但是如何定义“优先级”完全取决于我们自己。如果一个优先级队列记录的是医院里等待接受急救的病人,那么病人病情的严重性就是优先级。如果队列元素是银行的借贷业务,那么借记可能会优先于信贷。

priority_queue 模板有 3 个参数,其中两个有默认的参数;第一个参数是存储对象的类型,第二个参数是存储元素的底层容器,第三个参数是函数对象,它定义了一个用来决定元素顺序的断言。因此模板类型是:

 
  1. template <typename T, typename Container=std::vector<T>, typename Compare=std::less<T>> class priority_queue

如你所见,priority_queue 实例默认有一个 vector 容器。函数对象类型 less<T> 是一个默认的排序断言,定义在头文件 function 中,决定了容器中最大的元素会排在队列前面。fonction 中定义了  greater<T>,用来作为模板的最后一个参数对元素排序,最小元素会排在队列前面。当然,如果指定模板的最巵一个参数,就必须提供另外的两个模板类型参数。



图 1


图 1 中显示元素的方式反映了它们被检索的顺序。在 vector 中它们也可以不像这样排序。在讨论堆时,会解释原因。

创建 priority_queue

可以如下所示生成一个空的优先级队列:

 
  1. std::priority_queue<std::string> words;

可以用适当类型的对象初始化一个优先级队列:

 
  1. std::string wrds[] { "one", "two", "three", "four"};
  2. std::priority_queue<std::string> words { std::begin(wrds),std:: end(wrds)}; // "two" "three" "one" "four"

初始化列表中的序列可以来自于任何容器,并且不需要有序。优先级队列会对它们进行排序。

拷贝构造函数会生成一个和现有对象同类型的 priority_queue 对象,它是现有对象的一个副本。例如:

 
  1. std::priority_queue<std::string> copy_words {words}; // copy of words

也有带右值引用参数的拷贝构造函数,它可以移动一个实参对象。

当对容器内容反向排序时,最小的元素会排在队列前面,这时候需要指定 3 个模板类型参数:

 
  1. std:: string wrds[] {"one", "two", "three", "four"};
  2. std::priority_queue<std::string, std::vector<std::string>,std: :greater<std::string>> words1 {std::begin (wrds) , std:: end (wrds) }; //"four" "one" "three" "two"

这会通过使用 operator>() 函数对字符串对象进行比较,进而生成一个优先级队列,因此这会和它们在队列中的顺序相反。

优先级队列可以使用任何容器来保存元素,只要容器有成员函数 front()、push_back()、pop_back()、size()、empty()。这显然包含了 deque 容器,因此这里也可以用 deque 来代替:

 
  1. std::string wrds [] {"one", "two", "three", "four"};
  2. std::priority_queue<std::string, std::deque<std::string>> words {std::begin(wrds), std::end(wrds)};

这个 words 优先级队列在 deque 容器中保存了一些 wrds 数组中的字符串,这里使用默认的比较断言,因此队列中的元素会和上面 word1 中元素的顺序相同。priority_queue 构造函数会生成一个和第二个类型参数同类型的容器来保存元素,这也是 priority_queue 对象的底层容器。

可以生成 vector 或 deque 容器,然后用它们来初始化 priority_queue。下面展示了如何以 vector 的元素作为初始值来生成 priority_queue 对象:

 
  1. std::vector<int> values{21, 22, 12, 3, 24, 54, 56};
  2. std::priority_queue<int> numbers {std::less<int>(),values};

priority_queue 构造函数的第一个参数是一个用来对元素排序的函数对象,第二个参数是一个提供初始元素的容器。在队列中用函数对象对 vector 元素的副本排序。values 中元素的顺序没有变,但是优先级队列中的元素顺序变为:56 54 24 22 21 12 3。优先级队列中用来保存元素的容器是私有的,因此只能通过调用 priority_queue 对象的成员函数来对容器进行操作。构造函数的第一个参数是函数对象类型,它必须和指定的比较模板类型参数相同,函数对象类型默认是 less<T>。如果想使用不同类型的函数,需要指定全部的模板类型参数。例如:

 
  1. std::priority_queue<int, std::vector<int>,std::greater<int>> numbersl {std::greater<int>(), values};

第三个类型参数是一个比较对象类型。如果要指定这个参数,必须指定前两个参数——元素类型和底层容器类型。

priority_queue 操作

对 priority_queue 进行操作有一些限制:

  • push(const T& obj):将obj的副本放到容器的适当位置,这通常会包含一个排序操作。
  • push(T&& obj):将obj放到容器的适当位置,这通常会包含一个排序操作。
  • emplace(T constructor a rgs...):通过调用传入参数的构造函数,在序列的适当位置构造一个T对象。为了维持优先顺序,通常需要一个排序操作。
  • top():返回优先级队列中第一个元素的引用。
  • pop():移除第一个元素。
  • size():返回队列中元素的个数。
  • empty():如果队列为空的话,返回true。
  • swap(priority_queue<T>& other):和参数的元素进行交换,所包含对象的类型必须相同。


priority_queue 也实现了赋值运算,可以将右操作数的元素赋给左操作数;同时也定义了拷贝和移动版的赋值运算符。需要注意的是,priority_queue 容器并没有定义比较运算符。因为需要保持元素的顺序,所以添加元素通常会很慢。稍后会在堆(heaps)一节讨论 priority_queue 的内部操作。

以下展示了如何将键盘输入的数据记录到 priority_queue 中:

 
  1. std::priority_queue<std::string> words;
  2. std::string word; std::cout << "Enter words separated by spaces, enter Ctrl+Z on a separate line to end:\n";
  3. while (true)
  4. {
  5. if ((std::cin >> word).eof())
  6. break;
  7. words.push(word);
  8. }

按下 Ctrl+Z 组合键会在输入流中设置文件结束状态,因此可以用来结束循环输入。istream 对象的成员函数 operator>>() 返回一个输入流对象,因此我们可以用 if 条件表达式来调用 eof() 以检查 cin 的状态。这里会对输入单词进行排序,所以最大的单词总在 words 队列的前面——自动对输入单词排序。

priority_queue 没有迭代器。如果想要访问全部的元素,比如说,列出或复制它们,会将队列清空;priority_queue 和 queue 有相同的限制。如果想在进行这样的操作后,还能保存它的元素,需要先把它复制一份,这里可以使用一个不同类型的容器。下面展示了如何列出优先级队列 words 的内容:

 
  1. std::priority_queue<std::string> words_copy {words}; // A copy for output
  2. while (!words_copy.empty())
  3. {
  4. std:: cout << words_copy.top () <<" ";
  5. words_copy.pop();
  6. }
  7. std::cout << std::endl;

这里首先生成了一个 words 的副本,因为输出 words 会移除它的内容。输出 top() 返回的元素后,我们需要使用 pop() 来使下一个元素可访问。移除全部元素后,在循环条件中调用 empty() 以结束循环。也可以使用表达式 words_copy.size() 来控制循环,因为返回值会被隐式转换为布尔值,这样在 size() 返回 0 时,表达式的结果为 false。

如果为 words 输入:

one two three four five six seven
^Z

那么输出为:

two three six seven one four five

当然,如果需要多次输出 priority_queue 的内容,最好定义一个函数。这个函数应该是通用的,如下所示:

 
  1. template<typename T>
  2.  
  3. void list_pq(std::priority_queue<T> pq, size_t count = 5)
  4. {
  5. size_t n{count};
  6. while (!pq. empty())
  7. {
  8. std::cout << pq. top() << " ";
  9. pq.pop();
  10. if (--n) continue;
  11. std::cout << std::endl;
  12. n = count;
  13. }
  14. std::cout << std::endl;
  15. }

参数是以传值方式传入的,因此这里会处理一个优先级队列的副本。它是一个适用于任何类型容器的函数模板,只要容器实现了用于向 ostream 输出的 operator<<() 函数。如果没有设置第二个参数,默认每 5 个输出值一行。当然也可以定义一个适用于 queue 容器适配对象的函数模板。可以如下所示使用 priority_queue 的成员函数 emplace():

 
  1. words.emplace("nine");

以字符串为参数调用 string 类的构造函数会在容器的适当位置生成一个对象。这比下面的语句更有效率:

 
  1. words.push("nine");

这里编译器会在字符文字处插入一个 string 构造函数来生成 push() 的参数,然后以这个临时 string 对象作为参数调用 push()。push() 函数然后会调用 string 类的拷贝构造函数来将生成对象添加到容器中。我们把这些代码段组织成一个完整的程序:

 
  1. // Exercising a priority queue container adapter
  2. #include <iostream> // For standard streams
  3. #include <queue> // For priority_queue<T>
  4. #include <string> // For string class
  5. using std::string;
  6.  
  7. // List contents of a priority queue
  8. template<typename T>
  9. void list_pq(std::priority_queue<T> pq, size_t count = 5)
  10. {
  11. size_t n {count};
  12. while (!pq.empty())
  13. {
  14. std::cout << pq.top() << " ";
  15. pq.pop();
  16. if (--n) continue;
  17. std::cout << std::endl;
  18. n = count;
  19. }
  20. std::cout << std::endl;
  21. }
  22.  
  23. int main()
  24. {
  25. std::priority_queue<std::string> words;
  26. std::string word;
  27. std::cout << "Enter words separated by spaces, enter Ctrl+Z on a separate line to end:\n";
  28. while (true)
  29. {
  30. if ((std::cin >> word).eof())
  31. break;
  32. words.push(word);
  33. }
  34. std::cout << "You entered " << words.size() << " words:" << std::endl;
  35. list_pq(words);
  36. }

运行结果为:

Enter words separated by spaces, enter Ctrl+Z on a separate line to end:
one two three four five six seven eight nine ten eleven twelve
^Z
You entered 12 words:
two twelve three ten six
seven one nine four five
eleven eight

list_pq<T>() 函数模板实例的输出表明优先级队列对输出进行排序。

C++ queueC++堆 >

https://www.cnblogs.com/xzxl/p/7266404.html

GGBeng

编程,首先要多敲,其次才是学!

随笔- 517  文章- 0  评论- 6 

C++STL——优先队列

一、相关定义

优先队列容器与队列一样,只能从队尾插入元素,从队首删除元素。但是它有一个特性,就是队列中最大的元素总是位于队首,所以出队时,并非按照先进先出的原则进行,而是将当前队列中最大的元素出队。这点类似于给队列里的元素进行了由大到小的顺序排序。元素的比较规则默认按元素值由大到小排序,可以重载“<”操作符来重新定义比较规则。

优先级队列可以用向量(vector)或双向队列(deque)来实现(注意list container不能用来实现queue,因为list的迭代器不是任意存取iterator,而pop中用到堆排序时是要求randomaccess iterator 的!):
priority_queue<vector<int>, less<int> > pq1;     // 使用递增less<int>函数对象排序
priority_queue<deque<int>, greater<int> > pq2;   // 使用递减greater<int>函数对象排序
其成员函数有“判空(empty)” 、“尺寸(Size)” 、“栈顶元素(top)” 、“压栈(push)” 、“弹栈(pop)”等。

 

二、priority_queue

基本操作:

empty()      如果队列为空,则返回真

pop()    删除对顶元素,删除第一个元素

push()        加入一个元素

size()      返回优先队列中拥有的元素个数

top()     返回优先队列对顶元素,返回优先队列中有最高优先级的元素

在默认的优先队列中,优先级高的先出队。在默认的int型中先出队的为较大的数。

头文件:

#include <queue>

声明方式:

1、普通方法:

priority_queue<int> q;                 //通过操作,按照元素从大到小的顺序出队

priority_queue<int,vector<int>, greater<int> > q;    //通过操作,按照元素从小到大的顺序出队

2、自定义优先级:

struct cmp {     

  operator bool ()(int x, int y)     

  {        

     return x > y;   // x小的优先级高       //也可以写成其他方式,如: return p[x] > p[y];表示p[i]小的优先级高

  }

};

priority_queue<int, vector<int>, cmp> q;    //定义方法

//其中,第二个参数为容器类型。第三个参数为比较函数。

3、结构体声明方式:

struct node {     

  int x, y;     

  friend bool operator < (node a, node b)     

  {         

    return a.x > b.x;    //结构体中,x小的优先级高     

  }

};

priority_queue<node>q;   //定义方法

//在该结构中,y为值, x为优先级。

//通过自定义operator<操作符来比较元素中的优先级。

//在重载”<”时,最好不要重载”>”,可能会发生编译错误

 

三、代码实现

优先队列,其构造及具体实现我们可以先不用深究,我们现在只需要了解其特性,及在做题中的用法。
以一个例子来解释吧(呃,写完才发现,这个代码包函了几乎所有我们要用到的用法,仔细看看吧):

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

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

/*优先队列的基本使用    2017/8/1    xzxl*/ 

#include<stdio.h> 

#include<functional> 

#include<queue> 

#include<vector> 

using namespace std; 

//定义结构,使用运算符重载,自定义优先级1 

struct cmp1{ 

    bool operator ()(int &a,int &b){ 

        return a>b;//最小值优先 

    

}; 

struct cmp2{ 

    bool operator ()(int &a,int &b){ 

        return a<b;//最大值优先 

    

}; 

//定义结构,使用运算符重载,自定义优先级2 

struct number1{ 

    int x; 

    bool operator < (const number1 &a) const 

        return x>a.x;//最小值优先 

    

}; 

struct number2{ 

    int x; 

    bool operator < (const number2 &a) const 

        return x<a.x;//最大值优先 

    

}; 

int a[]={14,10,56,7,83,22,36,91,3,47,72,0}; 

number1 num1[]={14,10,56,7,83,22,36,91,3,47,72,0}; 

number2 num2[]={14,10,56,7,83,22,36,91,3,47,72,0}; 

   

int main() 

{   priority_queue<int>que;//采用默认优先级构造队列 

   

    priority_queue<int,vector<int>,cmp1>que1;//最小值优先 

    priority_queue<int,vector<int>,cmp2>que2;//最大值优先 

   

    priority_queue<int,vector<int>,greater<int> >que3;//注意“>>”会被认为错误, 

                                                      //这是右移运算符,所以这里用空格号隔开 

    priority_queue<int,vector<int>,less<int> >que4;最大值优先 

   

    priority_queue<number1>que5; 

    priority_queue<number2>que6; 

   

    int i; 

    for(i=0;a[i];i++){ 

        que.push(a[i]); 

        que1.push(a[i]); 

        que2.push(a[i]); 

        que3.push(a[i]); 

        que4.push(a[i]); 

    

    for(i=0;num1[i].x;i++) 

        que5.push(num1[i]); 

    for(i=0;num2[i].x;i++) 

        que6.push(num2[i]); 

   

   

    printf("采用默认优先关系:\n(priority_queue<int>que;)\n"); 

    printf("Queue 0:\n"); 

    while(!que.empty()){ 

        printf("%3d",que.top()); 

        que.pop(); 

    

    puts(""); 

    puts(""); 

   

    printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n"); 

    printf("Queue 1:\n"); 

    while(!que1.empty()){ 

        printf("%3d",que1.top()); 

        que1.pop(); 

    

    puts(""); 

    printf("Queue 2:\n"); 

    while(!que2.empty()){ 

        printf("%3d",que2.top()); 

        que2.pop(); 

    

    puts(""); 

    puts(""); 

    printf("采用头文件\"functional\"内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n"); 

    printf("Queue 3:\n"); 

    while(!que3.empty()){ 

        printf("%3d",que3.top()); 

        que3.pop(); 

    

    puts(""); 

    printf("Queue 4:\n"); 

    while(!que4.empty()){ 

        printf("%3d",que4.top()); 

        que4.pop(); 

    

    puts(""); 

    puts(""); 

    printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n"); 

    printf("Queue 5:\n"); 

    while(!que5.empty()){ 

        printf("%3d",que5.top()); 

        que5.pop(); 

    

    puts(""); 

    printf("Queue 6:\n"); 

    while(!que6.empty()){ 

        printf("%3d",que6.top()); 

        que6.pop(); 

    

    puts(""); 

    return 0; 

/*

运行结果 :

采用默认优先关系:

(priority_queue<int>que;)

Queue 0:

83 72 56 47 36 22 14 10  7  3

  

采用结构体自定义优先级方式一:

(priority_queue<int,vector<int>,cmp>que;)

Queue 1:

 7 10 14 22 36 47 56 72 83 91

Queue 2:

83 72 56 47 36 22 14 10  7  3

  

采用头文件"functional"内定义优先级:

(priority_queue<int,vector<int>,greater<int>/less<int> >que;)

Queue 3:

 7 10 14 22 36 47 56 72 83 91

Queue 4:

83 72 56 47 36 22 14 10  7  3

  

采用结构体自定义优先级方式二:

(priority_queue<number>que)

Queue 5:

 7 10 14 22 36 47 56 72 83 91

Queue 6:

83 72 56 47 36 22 14 10  7  3

*/ 

  

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值