• 【STL】关联式容器的使用(12)


    set/multiset容器

    与序列式容器的操作是很类似的:

    1. #include<iostream>
    2. #include<set>
    3. using namespace std;
    4. void main()
    5. {
    6. int x[]={1,2,3,4,5,6,1,2,5,7,8,9};
    7. set<int> s(x,x+sizeof(x)/sizeof(int));
    8. multiset<int> ms(x,x+sizeof(x)/sizeof(int));
    9. set<int>::iterator it;
    10. for(it=s.begin();it!=s.end();it++)
    11. cout<<*it<<" ";
    12. cout<<endl;
    13. multiset<int>::iterator mit;
    14. for(mit=ms.begin();mit!=ms.end();mit++)
    15. cout<<*mit<<" ";
    16. cout<<endl;
    17. }

    1、set/multiset 都实现了排序,set去掉了重复值,multiset 会保存重复值;

    2、没用提供随机访问,即不能通过下标对元素进行访问;

    3、可以通过begin 和 end  来查看最小值 或 最大值 :*(s.begin())   和 *(--s.end()),值得注意的是end不是最后一个元素;

    自定义排序:

    1. #include<iostream>
    2. #include <functional>
    3. #include<set>
    4. using namespace std;
    5. struct dst
    6. {
    7. float x;
    8. float y;
    9. };
    10. struct cmp
    11. {
    12. bool operator() (dst a,dst b)
    13. {
    14. return a.x*a.x+a.y*a.y < b.x*b.x+b.y*b.y ;
    15. }
    16. };
    17. void main()
    18. {
    19. int x[]={1,2,3,4,5,6,1,2,5,7,8,9};
    20. set<int,greater<int>> s(x,x+sizeof(x)/sizeof(int));
    21. set<int>::iterator it;
    22. for(it=s.begin();it!=s.end();it++)
    23. cout<<*it<<" ";
    24. cout<<endl;
    25. dst y[]={{1,2},{3,4},{1,0}};
    26. set<dst,cmp> t(y,y+sizeof(y)/sizeof(dst)); ;
    27. set<dst,cmp>::iterator it1;
    28. for(it1=t.begin();it1!=t.end();it1++)
    29. cout<<it1->x<<","<<it1->y<<" ";
    30. cout<<endl;
    31. }

    1、使用 greater 实现逆序,包含头文件functional;

    2、对结构体dst 进行自定义排序,注意:排序需要自定义的结构体,重载() 运算符;

    map/multimap容器

    1. #include<iostream>
    2. #include<map>
    3. #include<string>
    4. using namespace std;
    5. void main()
    6. {
    7. map<string,double> map1;
    8. map1["Tom"]=25.8;
    9. map1["Rose"]=15.2;
    10. map1.insert(pair<string,double>("Jack",25.8));
    11. map1.insert(make_pair("Bob",21.8));
    12. map1.insert(make_pair("Tom",12.4));
    13. map<string,double>::iterator it;
    14. for(it=map1.begin();it!=map1.end();it++)
    15. {
    16. cout<<it->first << ", " <<(*it).second<<endl;
    17. }
    18. cout<<"________________________________"<<endl<<endl;
    19. multimap<string,double> map2;
    20. map2.insert(make_pair("Tom",25.8));//map2["Tom"]=25.8;
    21. map2.insert(make_pair("Rose",15.2));//map2["Rose"]=15.2;
    22. map2.insert(pair<string,double>("Jack",25.8));
    23. map2.insert(make_pair("Bob",21.8));
    24. map2.insert(make_pair("Tom",12.4));
    25. map<string,double>::iterator it1;
    26. for(it1=map2.begin();it1!=map2.end();it1++)
    27. {
    28. cout<<it1->first << ", " <<(*it1).second<<endl;
    29. }
    30. }

     输出结果:

    1、C11后支持序列赋值进行初始化,如:

    map map1={{"Tom",25.8},{"Rose",15.2},{"Jack",25.8}};

    2、map支持直接赋值 :  map1["Tom"]=25.8;   multimap则不允许;

    3、可以利用pair 或者 直接使用make_pair 给 map/multimap 赋值( 用 insert函数);

    4、同样 map 只保存唯一的key  ,而 multimap 可以存入多个相同 key 的元素;(不是value)

    5、map/multimap 默认根据 key 进行排序,字符串使用 字典序 ;

    6、可以自定排序,类似set:

       map> map1;    //降序排列

    也可以定义排序,这里使用class来实现自定排序:

    1. class cmp
    2. {
    3. public:
    4. bool operator() (string a,string b)
    5. {
    6. return a>b;
    7. }
    8. };

    map map1;    //降序排列

    与结构体类型,需要重载操作符(),并且指定为public属性。

  • 相关阅读:
    【AI语言大模型】文心一言功能使用介绍
    YOLOv5、YOLOv8改进:ConvNeXt(backbone改为ConvNextBlock)
    计算机视觉面试题整理
    驱动开发 基于gpio子系统来实现对stm32开发板的led亮灭实现,附加定时器实现一秒亮灭(软件:vscode)
    iNFTnews|为什么说NFT门票是音乐行业的未来?
    最新版大学英语六级词汇打印版
    【Linux】:体系结构与进程概念
    nodejs系列:22.koa介绍
    DM8数据库批量创建表,插入数据,更改表信息,删除测试表
    复习四:栈
  • 原文地址:https://blog.csdn.net/yixiaobo2001/article/details/127753275