码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • c++ set、map的四种自定义排序方法


    c++ set、map的四种自定义排序方法

    • 一、set的三种遍历方式
    • 二、set自定义比较函数
      • 方式一:仿函数 重载运算符()
      • 方式二:普通函数
      • 方式三:类成员函数
      • 方式四:lambda表达式
    • 三、multiset自定义比较函数
    • 四、map自定义比较函数
    • 五、multimap自定义比较函数

    比如对于"100","99"要进行排序

    一、set的三种遍历方式

    set<string> myset={"99","100"};
    //set的三种遍历方式
    for(string s:myset) cout<<s<<' ';
    for(const string& s:myset) cout<<s<<' ';//set的值本身是不可修改的,使用引用就要加上const
    for(auto it=myset.begin();it!=myset.end();it++) cout<<*it<<' ';
    
    • 1
    • 2
    • 3
    • 4
    • 5

    可以发现,上述输出为"100",“99”,因为默认采用的less,按照字典序的从小到大,因为1比9小,所以1放前,如果要实现"99"放“100”前面,那么就要自定义比较函数了

    二、set自定义比较函数

    方式一:仿函数 重载运算符()

    注意要声明为const成员函数
    值传递

    struct cmp{
        bool operator()(string a,string b)const{
            return stoi(a)<stoi(b);
        }
    };
    set<string,cmp> myset={"99","100"};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    class cmp{
    public:
        bool operator()(string a,string b)const{
            return stoi(a)<stoi(b);
        }
    };
    set<string,cmp> myset={"99","100"};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    引用传递

    struct cmp{
        bool operator()(const string& a,const string& b)const{
            return stoi(a)<stoi(b);
        }
    };
    set<string,cmp> myset={"99","100"};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    class cmp{
    public:
        bool operator()(const string& a,const string& b)const{
            return stoi(a)<stoi(b);
        }
    };
    set<string,cmp> myset={"99","100"};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    后面的例子中,统一用引用传递了,不再重复了

    方式二:普通函数

    bool cmp(const string& a,const string& b){
        return stoi(a)<stoi(b);
    }
    int main(){
        set<string,decltype(&cmp)> myset(cmp);
        myset={"100","99"};
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    方式三:类成员函数

    类成员函数要加上static

    class Solution{
    public:
        static bool cmp(const string& a,const string& b){
            return stoi(a)<stoi(b);
        }
    
        void test(){
            set<string,decltype(&cmp)> myset(cmp);
            myset={"100","99"};
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    方式四:lambda表达式

    auto cmp=[](const string& a,const string& b){
            return stoi(a)<stoi(b);
        };
    set<string,decltype(cmp)> myset(cmp);
    myset={"100","99"};
    
    • 1
    • 2
    • 3
    • 4
    • 5

    三、multiset自定义比较函数

    以lambda表达式的写法为例, 说明只需要修改set为multiset,其他都是一摸一样的

    auto cmp=[](const string& a,const string& b){
        return stoi(a)<stoi(b);
    };
    multiset<string,decltype(cmp)> myset(cmp);
    myset={"100","99","100"};
    
    • 1
    • 2
    • 3
    • 4
    • 5

    四、map自定义比较函数

    仿函数 重载运算符()

    struct cmp{
        bool operator()(const string& a,const string& b)const{
            return stoi(a)<stoi(b);
        }
    };
    map<string,int,cmp> mp;
    mp={{"100",1},{"99",2}};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    lambda表达式

    auto cmp=[](const string& a,const string& b){
            return stoi(a)<stoi(b);
        };
    map<string,int,decltype(cmp)> mp(cmp);
    mp={{"100",1},{"99",2}};
    
    • 1
    • 2
    • 3
    • 4
    • 5

    c++ unordered_map4种遍历方式

    五、multimap自定义比较函数

    struct cmp{
        bool operator()(const string& a,const string& b)const{
            return stoi(a)<stoi(b);
        }
    };
    multimap<string,int,cmp> mp;
    mp={{"100",1},{"99",2}};
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    都是一个套路,记住了set的自定义排序方法
    也就等于会了set、multiset、map、multimap的自定义排序

  • 相关阅读:
    CDO如何盘点算法、推动算法业务增长
    新增动态排序图、桑基图、AntV组合图,DataEase开源数据可视化分析平台v1.18.10发布
    如何快速的把m4a转换成mp3格式
    stlink故障修复
    融云观察:AI Agent 是不是游戏赛道的下一个「赛点」?
    [SWPUCTF 2018]SimplePHP
    Codeforces 1255B. Fridge Lockers
    常见IO模型(非常详细)
    函数题14 习题6-3 使用函数输出指定范围内的完数 浙大版《C语言程序设计(第4版)》题目集
    添加docker容器数据卷
  • 原文地址:https://blog.csdn.net/qq_21539375/article/details/126595211
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号