• set和multiset容器


    1、基本概念

    所有元素在插入时会自动排好序;

    属于关联式容器,底层结构是用二叉树实现的

    2、set和multiset的区别

    set中不允许有重复元素,multiset允许有重复元素。

    3、构造和赋值

    构造:

    setst; //默认构造

    set; //拷贝构造函数

    赋值:

    重载等号操作符 =

    1. void printSet(set<int>&s)
    2. {
    3.     for (set<int>::iterator it = s.begin(); it != s.end(); it++)\
    4.     {
    5.         cout << *it << " ";
    6.     }
    7.     cout << endl;
    8. }
    9. void test01()
    10. {
    11.     set<int>s1;
    12.     //插入数据只能用 insert
    13.     s1.insert(10);
    14.     s1.insert(20);
    15.     s1.insert(5);
    16.     s1.insert(20);
    17.     printSet(s1);//5 10 20 自动排序 且去除重复元素
    18.     set<int>s2(s1);//拷贝构造
    19.     printSet(s2); //5 10 20
    20. }

    4、set大小和交换操作

    set容器不能重新指定大小

    函数原型:

    size(); //返回容器中的元素格式

    empty(); //判断是否为空

    swap(); // 交换两个集合容器

    1. void test02()
    2. {
    3.     set<int>s1;
    4.     //插入数据只能用 insert
    5.     s1.insert(1000);
    6.     s1.insert(200);
    7.     s1.insert(5);
    8.     s1.insert(180);
    9.     printSet(s1);//5 180 200 1000自动排序 且去除重复元素
    10.     if (s1.empty())
    11.     {
    12.         cout << "为空" << endl;
    13.     }
    14.     else
    15.     {
    16.         cout << "不为空" << endl;
    17.         cout << "容器大小为:" << s1.size() << endl;
    18.     }
    19.     set<int>s2;
    20.     s2.insert(9999);
    21.     s1.swap(s2);
    22.     cout << "交换后:" << endl;
    23.     printSet(s1);
    24. }

    4、set容器的插入与删除

    函数原型:

    clear();//清空

    erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器

    erase(beg,end); //删除区间[beg,end]的所有元素,返回下一个元素的迭代器

    erase(elem); //删除容器中值为elem的元素

    1. void test03()
    2. {
    3.     set<int>s1;
    4.     //插入数据只能用 insert
    5.     s1.insert(1000);
    6.     s1.insert(200);
    7.     s1.insert(5);
    8.     s1.insert(180);
    9.     printSet(s1);//5 180 200 1000自动排序 且去除重复元素
    10.     set<int>::iterator it = s1.begin();
    11.     it++; // 将it指向第二个元素
    12.     set<int>::iterator it1 = s1.end();
    13.     it1--; //指向倒数第二个元素
    14.     set<int>::iterator a;
    15.     a = s1.erase(it, it1); //预计返回指向最大元素的迭代器
    16.     cout << "*a=" << *a << endl; //1000
    17.     printSet(s1); //输出最大的元素和最小元素 5 1000
    18. }

    5、set容器的查找与统计

    函数原型:

    find(key); //查找key是否存在 若存在返回该键的元素的迭代器,若不存在,则返回set.end()

    count(key); //统计key的个数 0或1

    1. void test04()
    2. {
    3.     set<int>s1;
    4.     s1.insert(1000);
    5.     s1.insert(200);
    6.     s1.insert(5);
    7.     s1.insert(180);
    8.     s1.insert(30);
    9.     printSet(s1);//5 30 180 200 1000自动排序 且去除重复元素
    10.     set<int>::iterator pos = s1.find(30);
    11.     if (pos != s1.end())
    12.     {
    13.         cout << "找到了元素" << *pos << endl;
    14.     }
    15.     else
    16.     {
    17.         cout << "元素不存在" << endl;
    18.     }
    19.     int num = s1.count(180);
    20.     if (num == 0)
    21.     {
    22.         cout << "元素不存在" << endl;
    23.     }
    24.     else
    25.     {
    26.         cout << "元素个数为:" << num << endl;
    27.     }
    28. }

    6、set和multiset的区别

    set在insert会给一个pair类型的返回结果,而multiset不会,multiset会返回一个迭代器。

    1. void printMSet(multiset<int>&ms)
    2. {
    3.     for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)\
    4.     {
    5.         cout << *it << " ";
    6.     }
    7.     cout << endl;
    8. }
    9. void test05()
    10. {
    11.     set<int>s1;
    12.     s1.insert(1000);
    13.     s1.insert(200);
    14.     pairint>::iterator, bool>ret = s1.insert(10);
    15.     if (ret.second == true)
    16.     {
    17.         cout << "插入成功!"<
    18.     }
    19.     else
    20.     {
    21.         cout << "插入失败" << endl;
    22.     }
    23.     ret = s1.insert(10);
    24.     if (ret.second == true)
    25.     {
    26.         cout << "插入成功!" << endl;
    27.     }
    28.     else
    29.     {
    30.         cout << "插入失败"<
    31.     }
    32.     multiset<int>ms;
    33.     ms.insert(10);
    34.     ms.insert(10);
    35.     printMSet(ms);//10 10
    36. }

  • 相关阅读:
    uniapp小程序与webview通信
    1.mysql字段类型该怎么选择?
    DGIOT数字工厂工单结构介绍
    Java使用joml计算机图形学库,将3D坐标旋转正交投影转为2D坐标
    java计算机毕业设计ssm+vue二手手机销售平台
    【数据结构】树的概念与堆的实现
    mysql5.7.35安装配置教程【超级详细安装教程】
    Java基础简单整理
    HTTP协议
    你猜,怎么用一句话证明你是项目经理?
  • 原文地址:https://blog.csdn.net/qq_60143666/article/details/128000223