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

STL容器排序解析

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



### ### priority_queueC++ 中的使用方法 `priority_queue` 是 C++ STL 中的一种容器适配器,其底层基于堆结构实现,默认使用 `vector` 作为底层容器,并以 `operator<` 作为比较方式,构造最大堆,即每次访问的堆顶元素是当前集合中的最大值[^4]。 #### 基本构造方式 `priority_queue` 的构造函数可以接受三个模板参数: - 第一个参数是存储的元素类型。 - 第二个参数是底层容器类型,默认为 `vector`。 - 第三个参数是比较函数对象类型,默认为 `less<T>`,用于构造最大堆[^4]。 例如,构造一个默认的整数最大堆: ```cpp #include <queue> std::priority_queue<int> max_heap; ``` #### 自定义比较函数 若需要构造最小堆,可以传入 `greater<T>` 比较函数对象: ```cpp std::priority_queue<int, std::vector<int>, std::greater<int>> min_heap; ``` 对于自定义类型,如结构体或类,若其不支持直接比较,或需要特定的比较逻辑,则需要提供自定义仿函数[^1]。例如,定义一个结构体并自定义比较规则: ```cpp struct Node { int value; bool operator<(const Node& other) const { return value < other.value; // 默认构造最大堆 } }; std::priority_queue<Node> custom_heap; ``` #### 常用成员函数 - `push(const T& value)`:将元素插入堆中,并重新调整堆结构。 - `pop()`:移除堆顶元素,并重新调整堆结构。 - `top()`:访问堆顶元素。 - `empty()`:判断堆是否为空。 - `size()`:返回堆中元素数量。 示例: ```cpp std::priority_queue<int> heap; heap.push(10); heap.push(30); heap.push(20); while (!heap.empty()) { std::cout << heap.top() << " "; heap.pop(); } // 输出:30 20 10 ``` #### 底层容器选择 默认情况下,`priority_queue` 使用 `vector` 作为底层容器,因其提供了高效的随机访问和动态扩容能力。虽然也可以使用 `deque`,但在大多数场景下,`vector` 的性能更优[^2]。 #### 模拟实现原理 `priority_queue` 的模拟实现依赖于堆的基本操作,包括插入时的“上浮”操作和删除堆顶时的“下沉”操作。通过维护堆的结构,确保堆顶元素始终是最大值(最大堆)或最小值(最小堆)[^5]。 #### 使用场景 `priority_queue` 常用于需要快速访问优先级最高的元素的场景,例如任务调度、图算法(如 Dijkstra 算法)等。其基于堆的实现方式使得插入和删除操作的时间复杂度为 `O(log n)`,而访问堆顶元素的时间复杂度为 `O(1)`。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值