C++ STL中set/map 与 priority_queue 中greater、less 的用法区别

2018.3.4更新:


在自定义类的操作符重载的编写的时候,都写'<'符号,

在使用STL的set(map)中体现出小的在前面;

在使用priority_queue却提现出大的在前面。

代码如下:

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <list>
#include <string>

using namespace std;

class sst{
public:
    int x;
    int y;
    sst(int a, int b):x(a), y(b){}
};

bool operator<(const sst& a, const sst& b)
{
    return a.x < b.x;       //注意这里是'<',但是在map(set) 和 priority_queue中的排序方式却不同!
}

int main(void)
{
    //map
    map<sst, int> mp;
    for (int i = 0; i < 10; i++)
        mp[sst(i, i + 1)] = i * 5;
    cout<<"map :";
    for (auto it : mp)
        cout<<it.first.x<<"-"<<it.first.y<<"-"<<it.second<<" ";
    cout<<endl;
    
    //set
    set<sst> st;
    for (int i = 0; i < 10; i++)
        st.insert(sst(i, i + 1));
    cout<<"set :";
    for (auto it : st)
        cout<<it.x<<"-"<<it.y<<" ";
    cout<<endl;
    
    //priority_queue
    priority_queue<sst> pq;
    for (int i = 0; i < 10; i++)
        pq.push(sst(i, i + 1));
    cout<<"priority_queue :";
    while (!pq.empty())
    {
        cout<<pq.top().x<<"-"<<pq.top().y<<" ";
        pq.pop();
    }
    cout<<endl;
    
    return 0;
}

上面代码输出:

map :0-1-0 1-2-5 2-3-10 3-4-15 4-5-20 5-6-25 6-7-30 7-8-35 8-9-40 9-10-45 

set :0-1 1-2 2-3 3-4 4-5 5-6 6-7 7-8 8-9 9-10 

priority_queue :9-10 8-9 7-8 6-7 5-6 4-5 3-4 2-3 1-2 0-1 




**********************************************分割*******************************************************

总结:

set和map:底层都是红黑树 less<> 最小堆,greater<>是最大堆。 默认是less。

make_heap:  less<>() 展现出来的是最大堆, greater<>()展现出来是最小堆。  默认是less。

priority_queue:   底层是使用heap实现的,所以表现出来的特性和heap一致。 

                     less<>() 展现出来的是最大堆, greater<>()展现出来是最小堆。  默认是less。

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <list>
#include <string>

using namespace std;

void set_about(vector<int> t)
{
    //默认情况 = less<int> 最小堆(最小的在头部或者说顶部)
    multiset<int> mst;
    for (auto& i : t)
        mst.insert(i);
    cout<<"set less<int> : ";
    for (auto i : mst)
        cout<<i<<" ";
    cout<<endl;
    
    //greater<int> 最大堆(最大的在头部或者说顶部)
    multiset<int, greater<int>> mst2;
    for (auto& i : t)
        mst2.insert(i);
    cout<<"set greater<int> : ";
    for (auto i : mst2)
        cout<<i<<" ";
    cout<<endl;
}

void map_about(vector<int> t)
{
    //默认情况 = less<int> 最小堆(最小的在头部或者说顶部)
    multimap<int, int> mmp;
    for (auto& i : t)
        mmp.insert(make_pair(i, i));
    cout<<"map less<int> : ";
    for (auto i : mmp)
        cout<<i.first<<"-"<<i.second<<"  ";
    cout<<endl;
    
    //greater<int> 最大堆(最大的在头部或者说顶部)
    multimap<int, int,  greater<int>> mmp2;
    for (auto& i : t)
        mmp2.insert(make_pair(i, i));
    cout<<"map greater<int> : ";
    for (auto i : mmp2)
        cout<<i.first<<"-"<<i.second<<"  ";
    cout<<endl;
}

void heap_about(vector<int> t) //测试自带的heap工具
{
    //默认情况 是 最大堆
    cout<<"heap 默认 :";
    for (int i = 0; i < t.size(); i++)
    {
        make_heap(t.begin() + i, t.end(), greater<int>() );
        cout<<t[i]<<" ";
    }
    cout<<endl;
    
    //less<int>()  heap的less竟然是最大堆
    cout<<"heap less<int>() :";
    for (int i = 0; i < t.size(); i++)
    {
        make_heap(t.begin() + i, t.end(), less<int>() );
        cout<<t[i]<<" ";
    }
    cout<<endl;
    
    //greater<int>()  heap的greater竟然是最小堆
    cout<<"heap greater<int>() :";
    for (int i = 0; i < t.size(); i++)
    {
        make_heap(t.begin() + i, t.end(), greater<int>() );
        cout<<t[i]<<" ";
    }
    cout<<endl;
}

int main(void)
{
    vector<int> t = {4,5,1,1,1,6,2,7,3,8};
    set_about(t);
    map_about(t);
    heap_about(t);
    return 0;
}


set less<int> : 1 1 1 2 3 4 5 6 7 8 

set greater<int> : 8 7 6 5 4 3 2 1 1 1 

map less<int> : 1-1  1-1  1-1  2-2  3-3  4-4  5-5  6-6  7-7  8-8  

map greater<int> : 8-8  7-7  6-6  5-5  4-4  3-3  2-2  1-1  1-1  1-1  

heap 默认 :1 1 1 2 3 4 5 6 7 8 

heap less<int>() :8 7 6 5 4 3 2 1 1 1 

heap greater<int>() :1 1 1 2 3 4 5 6 7 8 



    priority_queue<int, vector<int>, greater<int>> pq;
    for (int i = 0; i < 10; i++)
        pq.push(i);
    while(!pq.empty())
    {
        cout<<pq.top()<<" ";           //注意priority_queue取头元素是 top();
        pq.pop();
    }

0 1 2 3 4 5 6 7 8 9



  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++ STLpriority_queue是一个优先队列,它是一个使用堆来实现的容器。它可以按照一定的优先级顺序存储元素,并且每次访问队首元素都是访问优先级最高的元素。 在使用priority_queue时,可以通过定义不同的比较函数来指定元素的优先级顺序。默认情况下,对于基本类型,默认是大顶堆,降序队列。也可以通过指定参数来实现小顶堆,升序队列。例如: priority_queue<int, vector<int>, greater<int>> q; //小顶堆,升序队列 priority_queue<int, vector<int>, less<int>> q; //大顶堆,降序队列 在对priority_queue进行操作时,可以使用push()函数向队列插入元素,使用top()函数获取队首元素,使用pop()函数删除队首元素。 在自定义类型的优先队列,可以重载运算符>或<来定义优先级。例如,可以重载operator>来定义小顶堆,即优先级较小的元素排在前面。示例代码如下: struct Node{ int x, y; Node(int a=0, int b=0): x(a), y(b) {} }; bool operator> (Node a, Node b){ if(a.x == b.x) return a.y > b.y; return a.x > b.x; } priority_queue<Node, vector<Node>, greater<Node>> q; q.push(Node(rand(), rand())); while(!q.empty()){ cout<<q.top().x<<' '<<q.top().y<<endl; q.pop(); } 总结来说,C++ STLpriority_queue是一个使用堆实现的优先队列,可以按照指定的优先级顺序存储元素。可以通过定义不同的比较函数或重载运算符来指定优先级规则。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [【总结】C++ 基础数据结构 —— STL之优先队列(priority_queue用法详解](https://blog.csdn.net/weixin_44668898/article/details/102132580)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值