3.8set/multiset容器

3.8.1set基本概念

简介:

  • 所有元素都会在插入时自动被排序

本质:

  • set/multiset属于关联式容器,底层结构是用二叉树实现

set和multiset区别:

  • set不允许容器中有重复的元素
  • multiset允许容器中有重复的元素

3.8.2 set构造和赋值

功能描述:创建set容器以及赋值

构造:

  • set<T> st;             //默认构造函数
  • set(const set&st); //拷贝构造函数

赋值:

  • set& operator=(const set &st); //重载等号操作符

#include <iostream>
using namespace std;
#include<set>
void printSet(const set<int> &s)
{
	for (set<int>::const_iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
//set容器的构造和赋值
void test01()
{
	set<int> s1;
	//插入数据 只有insert方式

	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(50);

	//遍历容器
	//set容器的特点,所有元素插入时自动被排序
	//set容器不允许插入重复值

	printSet(s1);

	//拷贝构造
	set<int>s2(s1);
	printSet(s2);

	//赋值
	set<int>s3;
	s3 = s2;
	printSet(s3);

}
int main()
{
	test01();
	system("pause");
	return 0;
}

总结:

  • set容器插入时用insert
  • set容器不允许插入重复的数值

3.8.3set大小和交换

功能描述:

  • 统计set容器大小以及交换set容器

函数原型:

  • size()        //返回容器中元素的数目
  • empty()    //判断容器是否为空
  • swap()     //交换两个集合容器

3.8.4 set插入和删除

功能描述

  • set容器进行插入数据和删除数据

函数原型

  3.8.5 set查找和统计

功能描述

  • 对set容器进行查找数据以及统计数据

函数原型

#include <iostream>
using namespace std;
#include<set>
//set容器的查找和统计
void test01()
{
	set<int> s1;
	s1.insert(20);
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);

	set<int>::iterator pos = s1.find(30);
	if (pos != s1.end())
	{
		cout << "找到元素:" << *pos << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
}
void test02()
{
	//统计
	set<int> s1;
	s1.insert(20);
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);

	//统计30的个数
	int num = s1.count(300);
	//对于set而言 统计个数不是零就是一
	cout << "元素个数为:" << num << endl;
}
int main()
{
	//test01();
	test02();
	system("pause");
	return 0;
}

3.8.6 set容器和mutiset容器的区别

#include <iostream>
using namespace std;
#include<set>
//Set容器和multiset容器区别
void test01()
{
	set<int>s1;

	s1.insert(10);
	//不允许插入重复值

	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();
	system("pause");
	return 0;
}

3.8.7pair对组的创建

功能描述:

  • 成对出现的数据,利用对组可以返回两个数据

两种创建方式:

  • pair<type,type> p (value1,value2);
  • pair <type,type> p=make_pair(value1,value2);

3.8.8set容器排序

学习目标:

  • set容器默认排序规则为从小到大,掌握如何改变排序规则

主要技术点:

  • 利用仿函数,可以改变排序规则

示例一:set存放int数据类型

#include <iostream>
using namespace std;
#include<set>

class myCompare
{
public:
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
//set容器排序
void test01()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(40);
	s1.insert(50);
	s1.insert(20);
	s1.insert(30);
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	//修改排序规则从大到小
	set<int, myCompare>s2;

	s2.insert(10);
	s2.insert(40);
	s2.insert(50);
	s2.insert(20);
	s2.insert(30);
	for (set<int, myCompare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

示例二:set存放自定义数据类型

#include <iostream>
using namespace std;
#include<set>
#include<string>

//set容器排序,存放自定义数据类型
class Person
{
public:
	Person(string name,int age,int weight)
	{
		this->m_Name = name;
		this->m_Age = age;
		this->m_Weight = weight;
	}
	string m_Name;
	int m_Age;
	int m_Weight;
};
class comparePerson
{
public:
	bool operator()(const Person&p1, const Person&p2)
	{


		//按照年龄降序
		if (p1.m_Age == p2.m_Age)
		{
			return p1.m_Weight > p2.m_Weight;
		}
		else
		{
			return p1.m_Age > p2.m_Age;
		}

	
	}
};
void test01()
{
	set<Person, comparePerson>s1;
	Person p1("乾哥", 22,125);
	Person p2("敏姐", 31,110);
	Person p3("广毓", 22,150);
	Person p4("蓉蓉", 21,102);


	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	s1.insert(p4);
	for (set<Person, comparePerson>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << "姓名为:" << (*it).m_Name << " 年龄为:" << (*it).m_Age <<" 体重为:"<<(*it).m_Weight<< endl;
	}
}
int main()
{
	test01();
	system("pause");
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值