cpp 优先队列 大顶堆 小顶堆 自定义排序规则 匿名函数 仿函数


int main() {
    //大顶堆
    std::priority_queue<int >q;  // 等同于 std::priority_queue<int,std::vector<int> , std::less<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);


    //------------------------自定义lambar比较------------------------------
// 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);

}


set

#include <bits/stdc++.h>
struct cmp {
    bool operator()(int x, int y) { return x > y; }
};
struct cmp1 {
    bool operator()(const int &x, const int &y) const { return x > y; }
};
int main() {
    set<int, cmp> st;
    st.insert(5);
    st.insert(3);
    cout << *st.begin() << '\n';
    return 0;
}
全部评论

相关推荐

点赞 评论 收藏
分享
06-12 10:50
门头沟学院 Java
你的不定积分没加C:我怎么在学院群看到了同样的话
点赞 评论 收藏
分享
昨天 12:17
已编辑
商丘师范学院 Java
后来123321:别着急,我学院本大二,投了1100份,两个面试,其中一个还是我去线下招聘会投的简历,有时候这东西也得看运气
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务