set/multiset容器

  1. set基本概念
  • 简介:所有元素都会在插入时自动被排序
  • 本质:set/multiset属于关联式容器,底层结构是二叉树实现的
  • set/multiset区别:(1)set不允许容器中有重复的元素(2)multiset容器中允许有重复的元素(3)set插入数据的同时会返回插入的结果,表示插入成功;multiset不会监测数据,因此可以插入重复数据。
  1. set构造和赋值
  • 创建set容器以及赋值
  • 构造:(1)set<T> st; (2)set(const set &st);
  • 赋值:set &operator = (const set &st);
    #include<iostream>
    #include<set>
    using namespace std;
    
    void printset(set<int>& s)
    {
    	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
    	{
    		cout << *it << " ";
    	}
    	cout << endl;
    }
    
    void test1()
    {
    	set<int> s;
    
    	//插入数据,只有insert方式
    	s.insert(1);
    	s.insert(2);
    	s.insert(3);
    	s.insert(4);
    	s.insert(2);
    
    	//遍历容器,所有元素插入时自动排序,不允许有重复的值,插入重复的数据无效
    	printset(s); //1 2 3 4
    
    	//拷贝构造
    	set<int>s1(s);
    	printset(s); //1 2 3 4
    
    	//赋值
    	set<int>s2;
    	s2 = s1;
    	printset(s2); //1 2 3 4
    
    
    	//multiset容器
    	multiset<int> ms;
    	ms.insert(6);
    	ms.insert(6);
    
    	for (multiset<int>::iterator it = ms.begin(); it != ms.end(); it++)
    	{
    		cout << *it << " ";
    	}
    	cout << endl; //遍历结果 6 6
    
    }
    int main()
    {
    	test1();
    	return 0;
    }

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值