stlset和multiset区别

stlset和multiset区别

学习目标:

掌握set和multiset的区别
区别:

set不可以插入重复数据,而multiset可以
set插入数据的同时会返回插入结果,表示插入成功
multiset不会监测数据,因此可以插入重复数据。

代码示例:

#include <iostream>
#include <set>
using namespace std;
//set和multiset区别
void test01()
{
   
       set<int>s;
       pair<set<int>::iterator, bool>ret = s.insert(10);
       if (ret.second)
       {
   
              cout << "第一次插入成功" << endl;
       }
       else
       {
   
              cout << "第一次插入失败" << endl;
       }
       ret=s.insert(10);
       if (ret.second)
       {
   
              cout << "第二次插入成功" << endl;
       }
       else
       {
   
              cout << "第二次插入失败" << endl;
       }
       multiset<int>ms;
       //允许插入重复值
       ms.insert(10);
       ms.insert(10);
       for (multiset<int>::iterator it = ms.begin(); it!= ms.end(); it++)
       {
   
              cout << *it << " ";
       }
       cout << endl;
}
int main()
{
   
       test01();
       return 0;
}

总结:

如果不允许插入重复值数据可以利用set

如果需要插入重复数据利用multiset

数据结构与算法 文章被收录于专栏

本专栏收录数据结构与算法知识点

全部评论

相关推荐

头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
07-01 23:23
郑州大学 Java
否极泰来来来来:牛客迟早有高三的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务