【c++】STL常用容器6—set / multiset容器


set基本概念

set容器也称为集合容器,所有元素都会在插入时自动被排序。

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

set/multiset区别:
set不允许容器中有重复的元素;
multiset允许容器中有重复的元素;
(这个是唯一的区别)

set构造和赋值

构造函数原型:

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

赋值函数原型:

set& operator=(const set& st);//重载等号操作符
#include<iostream>
using namespace std;
#include<set>

void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test()
{
	set<int> s1;

	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(50);
	s1.insert(70);
	s1.insert(20);
	s1.insert(50);//重复值

	//遍历容器
	//set容器特点:所有元素插入时自动被排序
	//set容器不允许插入重复值,multiset容器允许
	printSet(s1);

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

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

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

set大小和交换

函数原型:

size();//返回容器中元素的数目
empty();//判断容器是否为空
swap();//交换两个集合容器
//set容器不允许重新指定大小
#include<iostream>
using namespace std;
#include<set>

void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	set<int> s1;

	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(50);
	s1.insert(70);
	s1.insert(20);
	s1.insert(50);//重复值

	printSet(s1);

	if (s1.empty())
	{
		cout << "s1为空" << endl;
	}
	else
	{
		cout << "s1不为空" << endl;
		cout << "s1的大小为:" << s1.size() << endl;
	}
}

void test02()
{
	set<int> s1;
	s1.insert(10);
	s1.insert(50);
	s1.insert(70);
	s1.insert(20);

	set<int> s2;
	s2.insert(100);
	s2.insert(500);
	s2.insert(700);
	s2.insert(200);

	cout << "交换前:" << endl;
	printSet(s1);
	printSet(s2);

	cout << "交换后:" << endl;
	s1.swap(s2);
	printSet(s1);
	printSet(s2);
}

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

总结:
统计大小 — size
判断是否为空 — empty
交换容器 — swap

set插入和删除

函数原型:

insert(elem);//在容器中插入元素
clear();//清除所有元素
erase(pos);//删除pos迭代器所指的元素,返回下一个元素的迭代器
erase(beg,end);//删除区间[beg,end)的所有元素,返回下一个元素的迭代器
erase(elem);//删除容器中值为elem的元素
#include<iostream>
using namespace std;
#include<set>

void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	set<int> s1;

	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(50);
	s1.insert(70);
	s1.insert(20);

	//遍历
	printSet(s1);

	//删除
	s1.erase(s1.begin());
	printSet(s1);

	//删除重载版本
	s1.erase(50);
	printSet(s1);

	//清空
	s1.erase(s1.begin(), s1.end());
	//同s1.clear();
	printSet(s1);
}

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

总结:
插入 — insert
删除 — erase
清空 — clear

set查找和统计

函数原型:

find(key);//查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key);//统计key的元素个数,set容器不可以有重复元素,multiset容器可以有
#include<iostream>
using namespace std;
#include<set>

void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

//查找
void test01()
{
	set<int> s1;

	//插入数据
	s1.insert(10);
	s1.insert(50);
	s1.insert(70);
	s1.insert(20);

	//遍历
	printSet(s1);

	set<int>::iterator pos = s1.find(70);

	if (pos != s1.end())
	{
		cout << "找到元素:" << *pos << endl;
	}
	else
	{
		cout << "未找到元素" << endl;
	}
}

//统计
void test02()
{
	set<int> s1;

	//插入数据
	s1.insert(10);
	s1.insert(50);
	s1.insert(70);
	s1.insert(50);
	s1.insert(20);

	//遍历
	printSet(s1);

	//统计50的个数
	int num = s1.count(50);
	//对于set而言 统计结果 要么是0 要么是1
	cout << "num = " << num << endl;
}

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

set和multiset区别

区别:
1、set不可以插入重复数据,而multiset可以;
2、set插入数据的同时会返回插入结果,表示插入是否成功;
3、multiset不会检测数据,因此可以插入重复数据。

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

void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

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> m;
	//允许插入重复值
	m.insert(10);
	m.insert(10);
	for (multiset<int>::iterator it = m.begin(); it != m.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

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

总结:
1、如果不允许插入重复数据可以利用set;
2、如果需要插入重复数据利用multiset。

pair对组创建

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

两种创建方式:

pair<type,type> p (value1,value2);
pair<type,type> p = make_pair(value1,value2);
#include<iostream>
using namespace std;
#include<string>

void test()
{
	//第一种方式
	pair<string, int>p1("张三", 10);
	cout << "姓名:" << p1.first << " 年龄:" << p1.second << endl;

	//第二种方式
	pair<string, int>p2 = make_pair("李四", 20);
	cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}

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

set容器排序

set容器默认排序规则为从小到大。
利用仿函数,可以改变排序规则。

1、set存放内置数据类型

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

//仿函数
class mycompare
{
public:
	bool operator()(int v1, int v2) const//重载小括号()
	{
		return v1 > v2;
	}
};

//内置类型指定排序规则
void test01()
{
	set<int>s1;
	s1.insert(10);
	s1.insert(80);
	s1.insert(50);
	s1.insert(70);
	s1.insert(90);

	//默认从小到大排序
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;

	//指定排序规则从大到小,在插入数据前就指定排序规则
	set<int, mycompare>s2;
	s2.insert(10);
	s2.insert(80);
	s2.insert(50);
	s2.insert(70);
	s2.insert(90);

	for (set<int, mycompare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

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

2、set存放自定义数据类型

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

class person
{
public:
	person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
	string m_name;
	int m_age;
};

class compareperson
{
public:
	bool operator()(const person& p1, const person& p2)const
	{
		//按年龄降序
		return p1.m_age > p2.m_age;
	}
};

//内置类型指定排序规则
void test01()
{
	//自定义数据类型都会指定排序规则
	set<person,compareperson>s;
	
	//创建person对象
	person p1("张三", 21);
	person p2("李四", 22);
	person p3("王五", 23);
	person p4("老六", 24);

	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);

	//默认从小到大排序
	for (set<person,compareperson>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << "姓名:" << it->m_name << " 年龄:" << it->m_age << endl;
	}
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值