zoukankan      html  css  js  c++  java
  • 常用STL使用指北

    常用STL使用指北

    set和multiset

    set和multiset都是基于红黑树(显然是一个二叉搜索树)的STL。

    定义

    我们可以使用(multi)set<元素类型>名称来定义一个(multi)set

    自定义排序

    默认排序方式都是从小到大。因为结构体之间没有定义<,所以我们需要自定义一个比较函数。

    如果元素不是结构体:

    //自定义比较函数myComp,重载“()”操作符
    struct myComp
    {
    	bool operator()(const int &a,const int &b)
    	{
    		return a > b;//从大到小排序
    	}
    }
    set<int,myComp>s;
    set<int,myComp>::iterator it;
     
    
    

    如果元素是结构体:

    struct Info
    {
    	string name;
    	float score;
    	//重载“<”操作符,自定义排序规则
    	bool operator < (const Info &a) const
    	{
    		//按score从大到小排列
    		return a.score<score;
    	}
    }
     
    set<Info> s;
    set<Info>::iterator it;
    

    或者

    //自定义比较函数myComp,重载“()”操作符
    struct myComp
    {
    	bool operator()(const your_type &a,const your_type &b)
    	{
    		return a.data > b.data;//按照data从大到小排序
    	}
    }
    set<your_type,myComp>s;
    set<your_type,myComp>::iterator it;
    

  • 相关阅读:
    容器之队列的使用
    容器之栈的使用
    rapidxml的使用
    C++判断文件夹是否存在并创建文件夹
    VS2017,不能将const char *转为char *
    CSS_day01_选择器
    HTML_day02_列表
    HTML_day01基本标签
    python_day3
    python_day2
  • 原文地址:https://www.cnblogs.com/GavinZheng/p/11615650.html
Copyright © 2011-2022 走看看