STL:set/multiset容器详解

set/multiset属于关联式容器,底层结构使用二叉树实现的。它们的特点是:所有的元素在插入时会自动被排序。 而set与multiset容器的区别就是:set容器中不允许有重复的元素,而multiset允许容器中有重复的元素。

1.set构造和赋值

函数 描述
set<T> st 默认构造函数
set(const set &st) 拷贝构造函数
set& operator=(const set &st) 重载等号操作符

具体实现的代码如下所示:

#include<iostream>
#include<set>
using namespace std;
//set容器构造和赋值
void printVector(set<int>&s) {
    
    
	for (auto it = s.begin(); it != s.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}
int main() {
    
    
	set<int> s1;
	//该容器插入数据只有insert方式
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);
	s1.insert(30);
	//遍历容器
	//所有的元素在插入的时候会自动升序排序
	//set容器不允许插入重复值
	printVector(s1);//10 20 30 40
	//拷贝构造
	set<int> s2(s1);
	printVector(s2);

	//赋值
	set<int> s3;
	s3 = s2;
	printVector(s3);
	return 0;
}

这里需要注意,set容器插入时用insert,并且传入的是元素而不是迭代器。你可以这么理解,就是set容器中插入元素并不是顺序排列的,而是根据元素的大小进行顺序排列的,因此插入元素的位置只有在插入容器之后才能确定,因此不能传入迭代器。因此,这里需要将set集合的插入与vector容器的插入进行一定程度的区分。

2.set容器的大小与交换

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

具体代码的运用如下所示:

#include<iostream>
#include<set>
using namespace std;
void printSet(set<int>& s) {
    
    
	for (auto it = s.begin(); it != s.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}
int main() {
    
    
	set<int> s1;
	//插入数据
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	//打印容器
	printSet(s1);
	//判断容器是否为空
	if (s1.empty()) {
    
    
		cout << "s1为空" << endl;
	}
	else
	{
    
    
		cout << "s1不为空" << endl;
		cout << "s1的大小为:" << s1.size() << endl;
	}
	//交换容器
	set<int> s2;
	s2.insert(50);
	s2.insert(80);
	s2.insert(70);
	s2.insert(60);
	s2.insert(90);
	cout << "交换前:" << endl;
	printSet(s1);
	printSet(s2);

	s1.swap(s2);//交换容器
	cout << "交换后:" << endl;
	printSet(s1);
	printSet(s2);
	return 0;
}

3.set容器插入与删除

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

具体API的使用如下所示:

#include<iostream>
#include <set>
using namespace std;
void printSet(set<int> &s) {
    
    
	for (auto it = s.begin(); it != s.end(); it++) {
    
    
		cout << *it << " ";
	}
	cout << endl;
}
//set插入与删除
int main() {
    
    
	set<int> s1;
	//插入元素
	s1.insert(10);
	s1.insert(30);
	s1.insert(40);
	s1.insert(20);//插入后自动排序
	printSet(s1);
	//删除
	s1.erase(s1.begin());
	printSet(s1);
	//删除重载的版本
	s1.erase(30);
	printSet(s1);
	//清除
	s1.clear();//等价于s1.erase(s1.begin(), s1.end());
	printSet(s1);
	return 0;
}

4.set容器的查找与统计

函数 描述
find(key) 查找key是否存在,若存在返回该键的元素的迭代器;若不存在,返回set.end()
count(key) 统计key的元素个数
#include<iostream>
#include <set>
using namespace std;
//set容器的查找与统计
int main() {
    
    
	set<int> s1;
	//插入数据
	s1.insert(20);
	s1.insert(30);
	s1.insert(10);
	s1.insert(40);
	//查找
	set<int>::iterator pos = s1.find(30);
	if (pos!= s1.end()) {
    
    //找到元素
		cout << "找到该元素:" << *pos<< endl;
	}
	else {
    
    
		cout << "未找到该元素" << endl;
	}
	//统计
	//这里需要注意 set容器不予许出现重复的元素
	//因此count返回的值只能为0或者1
	//统计30的个数
	s1.insert(30);
	s1.insert(30);
	int num = s1.count(30);
	cout << "30的个数为:" << num << endl; //1
	return 0;
}

这里主要注意,由于set容器不允许出现重复的元素,因此统计count函数只能返回0或者1,因此count函数可以用来判断某个元素是否存在与set容器中。

5.set与multiset容器的区别

二者的区别如下所示:
1.set容器不予许出现重复数据,而multiset可以;
2.set插入数据的同时会返回插入结果,表示插入成功;
3.multiset不会检测数据,因此可以插入重复数据。

#include<iostream>
#include<set>
using namespace std;
int main() {
    
    
	set<int>s;
	pair<set<int>::iterator,bool> res=s.insert(10);
	if (res.second) {
    
    
		cout << "第一次插入10成功";
	}
	else {
    
    
		cout << "第一次插入10失败";
	}
	cout << endl;
	res = s.insert(10);
	if (res.second) {
    
    
		cout << "第二次插入10成功";
	}
	else {
    
    
		cout << "第二次插入10失败";
	}
	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;
	return 0;
}

这里需要注意,对于set容器的插入会返回一个pair二元组得的数据类型,而multiset容器的插入只会返回一个迭代器。

6.pair对组创建

成对出现的数据,利用对组可以返回两个数据。pair对组主要有两种创建方式。
两种创建方式如下:
pair<type,type> p(value1,value2);
pair<type,type> p=make_pair(value1,value2);

#include<iostream>
#include<string>
using namespace std;
//pair对组的使用
int main() {
    
    
	//第一种创建方式
	//pair<type,type> p(value1,value2);
	pair<string, int>p("Tom", 20); 
	cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
	//第二种方式创建对组
	//pair<type,type> p=make_pair(value1,value2);
	pair<string, int> p2 = make_pair("Nancy", 22);
	cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_51447436/article/details/126687920
今日推荐