(C++)set/multiset 容器基础知识

本文介绍了C++中set和multiset的基础知识,包括它们的基本概念、构造与赋值、大小和交换操作、插入与删除元素、查找与统计功能。同时,详细讨论了set与multiset的区别,以及如何使用pair对组创建和管理数据。此外,文章还涵盖了set容器的排序机制,包括内置数据类型的升序和降序排序,以及自定义数据类型的排序方法。

3.8 set/multiset 容器

3.8.1 set基本概念

本质:

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

set和multiset区别:

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

3.8.2 set构造和赋值


#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) << "  " << endl;
	}
}
void test01()
{
	set<int> s1;

	s1.insert(30);
	s1.insert(10);
	s1.insert(20);
	s1.insert(40);
	s1.insert(20);
	//set容器特点
	//1.所有元素插入的时候会自动被排序
	//2.set容器不允许插入重复值
	printSet(s1);

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

	//赋值
	set<int>s3;
	s3 = s2;
	printSet(s3);
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3.8.3 set大小和交换


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

3.8.4 set插入和删除


	函数原型:
	
		insert(elem);		//在容器中插入元素
		clear();			//清除所有元素
		erase(pos);			//删除pos迭代器所指的元素,返回下一个元素的迭代器
		erase(beg,end);		//删除区间内的所有元素,返回下一个元素的迭代器
		erase(elem);		//删除容器中值为elem的元素

3.8.5 set查找和统计

	
	函数原型:
	
		find(key);			//查找key是否存在,若存在,返回该元素的迭代器;若不存在,返回set.end()
		count(key);			//统计key的元素个数

————————————————————————————————————————————————————————————————————————————————————————————
#include <set>
#include <iostream>
using namespace std;
void test01()
{
	set<int> s1;

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

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

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

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

	int num = s1.count(30);
	//统计结果要么是1要么是0
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3.8.6 set和multiset区别

区别:

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

#include <set>
#include <iostream>
using namespace std;
void test01()
{
	set<int>s;

	pair<set<int>::iterator,bool> ret = s.insert(10);
	
	//取第二个数据类型,即bool型,判断是否插入成功
	if(ret.second)
	{
		cout << "Success" << endl;
	}
	else
	{
		cout << "Fail" << endl;
	}


	multiset<int>ms;
	ms.insert(10);
	ms.insert(10);
	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.7 pair对组创建

功能描述:
成对出现的数据,利用对组可以返回两个数据

两种创建方式:


	pair<type, type> p ( value1,value2 );
	pair<type, type> p = make pair ( value1, value2 );
	
—————————————————————————————————————————————————————————————————————————————————————————————

#include <string>
#include <iostream>
using namespace std;
void test01()
{
	//1.
	pair<string,int>p ("Tom", 20);
	cout << "姓名:" << p.first << "   年龄:" << p.second << endl;

	//2.
	pair<string,int>p2 = make_pair("Jerry",19);
	cout << "姓名:" << p2.first << "   年龄:" << p2.second << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

3.8.8 set容器排序

1.内置数据类型升降序排序

#include <iostream>
#include <string>
#include <set>
using namespace std;
class MyCompare
{
public:
	bool operator()(int v1, int v2)
	{
		return v1>v2;
	}
};
void test01()
{
	set<int> s1;

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

	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(20);
	s2.insert(30);
	s2.insert(50);

	for(set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

2.自定义数据类型排序


/*
## 3.8.8 set容器排序



*/

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

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)
	{
		return p1.m_Age > p2.m_Age;
	}
};

//存放自定义数据类型排序
void test02()
{
	//自定义的数据类型,都会指定排序规则
	set<Person,comparePerson> s;

	Person p1("kk",21);
	Person p2("bb",22);
	Person p3("cc",23);
	Person p4("dd",25);

	
	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()
{
	test02();
	system("pause");
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值