码农知识堂 - 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的自定义排序

  • 相关阅读:
    可扩展标记语言——XML
    广东长荣科技有限公司-Java笔试题
    zookeeper集群环境搭建
    【网络安全 --- 工具安装】VMware 16.0 详细安装过程(提供资源)
    局部变量,全局变量与内存
    idea springboot 自定义注释无效
    在PyCharm中直接启动mitmproxy并自动打开&关闭系统代理
    大屏开发,浏览器的可视区域和设备的分辨率
    前端性能优化——启用文本压缩
    华为云Centos7搭建hadoop集群一:云服务器准备
  • 原文地址:https://blog.csdn.net/qq_21539375/article/details/126595211
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号