zoukankan      html  css  js  c++  java
  • 1、常用C++STL集合

    集合

    一、集合中常见操作函数

    1、集合的组成

    集合是由不重复元素组成的,例如{1,2,3},就是由数据1、2、3组成的集合

    2、集合头文件

    #include<set>

    using namespace std;

    3、集合的定义

    C++定义一个集合的语句是:set<T> s,定义了一个名为s,存储数据类型为T的集合,初始是空集合,我们可以定义set<int> a,set<string> aa等等

    4、集合插入元素

    set 使用insert向集合中插入一个新元素,如果元素已经存在,那么这个插入是无效的,因为集合中不存在重复元素;

    #include<set>
    using namespace std;
    int main(){
    set<string> country;      //{}
    country.insert("china");     //{"china"}
    country.insert("American");     //{"china","American"}
    country.insert("china"); {"china","American"};      //“China”插入无效
    return 0;
    }

    5、集合删除元素

    集合set使用erase从集合中删除一个元素,如果集合中不存在这个元素,则不进行任何操作

    #include<set>
    using namespace std;
    int main(){
    set<string> country;      //{}
    country.insert("china");     //{"china"}
    country.insert("American");     //{"china","American"}
    country.erase("china"); //{"American";
    country.erase("china"); //删除“china”无效,因为集合中不存在这样的元素
    return 0;
    }

    6、集合判断元素是否存在

    C++如果你想判断一个元素是否在集合中存在,可以使用count函数,存在返回1,不存在返回0

    #include<set>
    using namespace std;
    int main(){
    set<string> country;      //{}
    country.insert("china");     //{"china"}
    country.insert("American");     //{"china","American"}
    if(country.count("china")){   //判断“china”是否属于country
    cout << "china belong to country " << endl;
    }
    return 0; }

    7、集合迭代器

    C++可以通过迭代器来访问各个元素,迭代器的写法是:set<T>::iterator it 定义了一个指向set<T>的迭代器it,其中T是任意数据类型,::iterator是固定类型

    begin函数返回容器中起始元素的迭代值,end函数返回容器中尾部元素的迭代值

    通过*操作可以获取指定的元素,通过++可以获取下一个元素,--可以获取上一个元素

    注意:使用集合内部是一个按照从小到大有序的状态,所以迭代器在输出的时候是一个有序序列

    #include<set>
    #include<string>
    #include<iostream>
    using namespace std;
    int main(){
    set<string> country;    //{}
    country.insert("china");   //{"china"}
    country.insert("American"); // {"china","American"}
    for(set<string>::iterator it = country.begin();it != country.end();it++){
    cout << *it << endl;    //通过迭代器输出对象
    }
    return 0;
    }

    8、清空集合内存和元素

    C++中调用clear()函数就可以清空集合元素,同时清空set占用的内存

    C++中调用size()函数就可以获取集合元素的个数

     9、定义重载小于的结构体

     1 struct Node{
     2 int x,y;
     3 bool  operator<(const Node &rhs) const{  //函数名:operator<      rhs:右操作数,定义为一个const引用     运算符重载定义在结构体内部,左操作数就调用operator<的对象
    最后的const函数表示不能对其数据成员进行修改,同时const对象只能调用const成员函数
    4 if(x == rhs.x){ 5 return y < rhs.y; 6 }else{ 7 return x < rhs.x; 8 } 9 }
    //上面重载定义的排序函数,按照x从小到大排,x相等的时候,按照y从小到大排。可以直接比较两个Node对象,然后直接存储在set中
    10 }; 11

    二、多重集合

    1、多重集合定义

    多重集合:同一个元素可以出现多次 ,例如集合{1,1,1,2,2,2}不是一个集合,而是一个多重集合,一个元素在多重集合中出现的次数称为(重复度或重次)

    C++中多重集合为multiset,定义在头文件<set> 中,相关函数都和set是一样的

    2、多重集合的函数操作

    #include<iostream>
    #include<multiset>
    using namespace std;
    int main(){
        multiset<int> ms; //初始集合为空{}
        ms.insert(5);  //插入元素5,{5}
        ms.insert(5);  //插入元素5,{5,5}
        ms.insert(4);  //插入元素4,{4,5,5}
        ms.insert(4);  //插入元素4,{4,4,5,5}     可重复插入新元素
        ms.insert(6);  //插入元素6,{4,4,5,5,6}
        ms.erase(4);   //删除元素4,{5,5,6}       若删除的元素在集合中重复出现,则删除所有这个元素
        ms.erase(3);   //删除元素3,{5,5,6}       因为元素3在集合中不存在,所以在执行erase操作的过程中,不会有任何变化
        //这么多数,怎么删除第一个5呢,引入一个函数find
        //find函数返回指向第一个值为x 的迭代器,如果x不在迭代器中,则返回尾迭代器
        multiset<int>::iterator it; //定义迭代器
        it = ms.find(5);  //找到元素5开始的迭代器
        cout << *it << endl;  //输出5 的值
        it = ms.find(6);  //因为元素6在集合中不存在,所以返回尾迭代器end()
        cout << ms.count(5) << " "; //集合中有2个元素5
        ms.erase(ms.find(5));   //首先迭代器找到第一个元素5的位置,然后去掉第一个5
        cout << ms.count(5) << " "; //集合中有1个元素5
        //lower_bound  返回第一个大于等于元素x的迭代器的位置,如果没有找到返回的尾迭代器
        //upper_bound  返回第一个大于元素X的迭代器的位置,如果没有找到返回的是尾迭代器
        //集合{5,5,6}
        cout << *ms.lower_bound(5) << endl; //容器第一个大于等于5 的元素是5
        cout << *ms.upper_bound(5) << endl; //容器第一个大于5的元素是6
    }

    3、

  • 相关阅读:
    leetcode每日刷题计划-简单篇day10
    leetcode每日刷题计划-简单篇day9
    leetcode每日刷题计划-简单篇day8
    leetcode每日刷题计划-简单篇day7
    leetcode每日刷题计划-简单篇day6
    leetcode每日刷题计划-简单篇day5
    leetcode每日刷题计划-简单篇day4
    leetcode每日刷题计划-简单篇day3
    设计模式解决 if-else
    线程池
  • 原文地址:https://www.cnblogs.com/xiaoming521/p/15679257.html
Copyright © 2011-2022 走看看