STL set容器 multiset容器

12 篇文章 0 订阅
9 篇文章 0 订阅

STL中的容器:

set容器 multiset容器 常用接口及用法:


set / multiset 容器:也被称为集合,所有元素会在插入时自动排序(从小到大排,即升序)

set / multiset 容器的底层结构:这两个容器属于关联式容器,底层结构是用二叉树实现的;


setmultiset 的区别:

1.set 容器中不允许重复的元素;

2.multiset 容器中是可以重复的元素的;


各种函数接口具体如何使用,下面的代码块中会有详细的使用方法


setmultiset 容器的构造函数:

1.set<T> st; 默认(无参)构造函数

2.set(const set & st); 拷贝构造函数

#include <iostream>
#include <set>  //使用STL中的容器,得包含它的头文件,set和multiset容器的头文件是一样的,都是set 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

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

void test_1()  //set和multiset容器的构造函数 
{
	set<int> s1;  //默认构造函数
	
	//set和multiset容器的插入只能使用insert,不可以使用push_back()等等
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(50);
	s1.insert(20);
	s1.insert(40);
	
	//遍历输出打印
	//set容器特点:数据插入时会自动排序(从小到大排)
	//set容器不允许有重复的数据出现,因此20只会出现一次 
	//10 20 30 40 50
	printSet(s1);
	
	set<int> s2(s1);  //拷贝构造函数
	printSet(s2); 
}

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

setmultiset 容器的赋值:

1.set& operator=(const set & st); 通过重载赋值运算符的方式给新创建的 set 容器赋值;

#include <iostream>
#include <set>  //使用STL中的容器,得包含它的头文件,set和multiset容器的头文件是一样的,都是set 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

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

void test_1()  //set和multiset容器的赋值 
{
	set<int> s1;
	
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(50);
	s1.insert(20);
	s1.insert(40);
	
	printSet(s1);
	
	set<int> s2;
	s2 = s1;  //通过重载赋值运算符的方式给新创建的set容器赋值 
	printSet(s2);
}

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

setmultiset 容器的大小和交换:


setmultiset 容器的大小:

1.empty(); 判断 set 容器是否为空;

2.size(); 用于查看容器的大小(元素个数);


setmultiset 容器的交换:

1.swap(st); 将容器 st 与本身的 set 容器进行交换;


#include <iostream>
#include <set>  //使用STL中的容器,得包含它的头文件,set和multiset容器的头文件是一样的,都是set 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

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

void test_1()  //set和multiset容器的大小 
{
	set<int> s1;
	
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(50);
	s1.insert(40);
	
	printSet(s1);
	
	if(s1.empty())  //判断当前容器是否为空,如果容器为空,则返回true,否则返回false
	{
		cout << "s1容器为空" << endl;
	}
	else
	{
		cout << "s1容器不为空" << endl;
		cout << "s1的大小为:" << s1.size() << endl;  //用于查看容器的大小(元素个数)
	}
}

void test_2()  //set和multiset容器的交换 
{
	set<int> s1;
	
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(50);
	s1.insert(40);
	
	set<int> s2;
	
	s2.insert(100);
	s2.insert(300);
	s2.insert(200);
	s2.insert(500);
	s2.insert(400);
	
	cout << "交换前:" << endl;
	printSet(s1);
	printSet(s2);
	
	s1.swap(s2);  //将s1和s2进行交换 
	cout << "交换后:" << endl;
	printSet(s1);
	printSet(s2);
}

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

setmultiset 容器的插入和删除:

1.insert(elem);set 容器中插入一个 elem 元素;

2.erase(elem); 删除 set 容器中与元素 elem 匹配的元素;

4.erase(pos); 通过迭代器删除掉 set 容器指定位置的数据;

5.erase(begin,end); 通过迭代器删除掉 set 容器区间 [begin,end) 之间的数据;

6.clear(); 清空当前的 set 容器;

#include <iostream>
#include <set>  //使用STL中的容器,得包含它的头文件,set和multiset容器的头文件是一样的,都是set 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

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

void test_1()  //set和multiset容器的插入和删除 
{
	set<int> s1;
	
	//set容器的插入 
	s1.insert(30);
	s1.insert(10);
	s1.insert(20);
	s1.insert(50);
	s1.insert(40);
	
	printSet(s1);
	
	s1.erase(s1.begin());  //通过迭代器删除掉set容器指定位置的数据
	//20 30 40 50
	printSet(s1);
	
	s1.erase(30);  //删除set容器中与30匹配的数据
	//20 40 50
	printSet(s1);
	
	s1.erase(s1.begin(),s1.end());  //通过迭代器删除掉set容器区间[s1.begin(),s1.end())之间的数据
	printSet(s1);
	
	s1.clear();  //清空当前的容器
	printSet(s1); 
}

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

setmultiset 容器的统计和查找:


set 容器的查找:

1.find(key);set 容器中查找 元素key 是否存在,若存在,返回该元素的迭代器;若不存在,返回set.end()

元素 key 不存在,返回 set.end() 可以理解成找到 set 容器的最后一个元素都没有找到元素 key,因此返回末尾位置的迭代器;


set 容器的统计:

1.count(key);set 容器中统计元素 key 出现的次数,返回值即为元素 key 出现的次数;

对于 set 容器,只可能返回 0 或者 1;对于 multiset 容器,返回值可以大于 1

因为 set 容器中的元素不可以重复,但 multiset 容器的元素是允许重复的。也就是说,在 set 容器中,元素 key 最多出现一次,所以 set 容器只能返回 0 或者 1


#include <iostream>
#include <set>  //使用STL中的容器,得包含它的头文件,set和multiset容器的头文件是一样的,都是set 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

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

void printMultiset(const multiset<int> & s)
{
	for(multiset<int>::const_iterator it=s.begin();it!=s.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test_1()  //set和multiset容器的查找 
{
	set<int> s1;
	 
	s1.insert(30);
	s1.insert(10);
	s1.insert(20);
	s1.insert(50);
	s1.insert(40);
	
	set<int>::iterator pos = s1.find(30);  //用迭代器接收find()函数的返回值
	
	if(pos==s1.end()) 
	{
		cout << "没有找到目标元素" << endl;
	}
	else
	{
		cout << "找到目标元素:" << *pos << endl;
	}
}

void test_2()  //set和multiset容器的统计 
{
	set<int> s1;  //set容器s1 
	
	s1.insert(30);
	s1.insert(10);
	s1.insert(20);
    s1.insert(30);
	s1.insert(50);
	s1.insert(40);
    s1.insert(30);
	
	printSet(s1);
	
	int num_1 = s1.count(30);  //num_1为30在set容器中出现的次数
	
	cout << "目标元素出现次数为:" << num_1 << endl;
	
	
	
	multiset<int> s2;  //multiset容器s2 
	
	s2.insert(30);
	s2.insert(10);
	s2.insert(20);
	s2.insert(30);
	s2.insert(50);
	s2.insert(40);
	s2.insert(30);
	
	printMultiset(s2);
	
	int num_2 = s2.count(30);  //num_2为30在multiset容器中出现的次数
	
	cout << "目标元素出现次数为:" << num_2 << endl;
}

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

setmultiset 的区别:

1.set 不可以插入重复的数据,但 multiset 可以;

2.set 插入数据时,同时会返回插入的结果,告诉你插入操作是否成功;

set 容器返回的结果是一个对组,对组里面有两样东西,一个是迭代器,另一个是一个 bool 类型的量,它用来判断是否插入成功;

3.multiset 容器插入数据时,不会检测数据是否重复插入,因此multiset容器可以插入相同的数据;

multiset 容器返回的仅仅是一个迭代器,因此不存在对插入操作进行检测,可以插入相同的数据;

#include <iostream>
#include <set>  //使用STL中的容器,得包含它的头文件,set和multiset容器的头文件是一样的,都是set 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

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

void printMultiset(const multiset<int> & s)
{
	for(multiset<int>::const_iterator it=s.begin();it!=s.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

void test_1()  //set和multiset容器的区别
{
	set<int> s;
	
	pair<set<int>::iterator,bool> ret = s.insert(50);  //用一个pair对组接收insert()的返回值
	
	if(ret.second)  //取出对组的第二样东西,即一个bool类型的量
	{
		cout << "第一次插入50成功" << endl;
	}
	else
	{
		cout << "第一次插入50失败" << endl;
	}
	
	ret = s.insert(50);
	
	if(ret.second)
	{
		cout << "第二次插入50成功" << endl;
	}
	else
	{
		cout << "第二次插入50失败" << endl;
	}
	
	multiset<int> ms;
	
	ms.insert(50);
	ms.insert(50);
	ms.insert(50);
	ms.insert(50);
	ms.insert(50);
	
	//50 50 50 50 50 multiset容器允许插入相同的数据 
	printMultiset(ms);
}

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

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

pair 对组的创建方式:

1.pair<T1,T2> p(value_1,value_2); 有参构造函数

2.pair<T1,T2> p = make_pair(value_1,value_2); 利用成员函数 make_pair() 创建对组;

#include <iostream>
#include <string>

using namespace std;

void test_1()  //pair对组的创建 
{
	pair<string,int> p1("Yauge",18);  //有参构造函数
	
	cout << "姓名:" << p1.first << " 年龄:" << p1.second << endl;  //通过first和second分别得到第一个数据和第二个数据
	
	pair<string,int> p2 = make_pair("Yauge",18);  //利用成员函数make_pair()创建对组 
	
	cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;  //通过first和second分别得到第一个数据和第二个数据
}

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

setmultiset 容器指定排序规则:(对内置数据类型)

利用仿函数(重载 () 运算符)的技术,修改 set 容器的排序规则(set 容器默认排序规则为从小到大,我们人为修改为从大到小);

但因为 set 容器在插入时会自动的做排序,所以要修改默认排序规则的话,应该要在插入操作前修改排序规则;

#include <iostream>
#include <set>  //使用STL中的容器,得包含它的头文件,set和multiset容器的头文件是一样的,都是set 
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

using namespace std;

class MyCompare  //制定排序规则
{
public:
	bool operator()(int v1,int v2)  //利用仿函数(重载()运算符)的技术,实现默认排序规则的修改
	{
		return v1 > v2;  //排序规则修改为从大到小排 
	}
};

void test_1()  //set容器修改排序规则(对内置数据类型)
{
	set<int> s1;
	
	s1.insert(20);
	s1.insert(10);
	s1.insert(50);
	s1.insert(30);
	s1.insert(40);
	
	cout << "排序规则修改前:" << endl; 
	for(set<int>::const_iterator it=s1.begin();it!=s1.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
	
	//因为set容器在插入时会自动的做排序,所以要修改默认排序规则的话,应该要在插入操作前修改排序规则
	
	set<int,MyCompare> s2;   //利用仿函数(重载()运算符)的技术,实现默认排序规则的修改
	
	s2.insert(20);
	s2.insert(10);
	s2.insert(50);
	s2.insert(30);
	s2.insert(40);
	
	cout << "排序规则修改后:" << endl;
	for(set<int,MyCompare>::iterator it=s2.begin();it!=s2.end();it++)
	{
		cout << *it << " ";
	}
	cout << endl;
}

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

setmultiset 容器指定排序规则:(对自定义数据类型)

在使用到 setmultiset 容器存放自定义数据类型时,需要提前指定好排序规则,否则编译器不知道怎么将我们的自定义数据插入到 setmultiset 容器中;(因为 setmultiset 容器在插入数据时会自动的做排序操作)

#include <iostream>
#include <set>  //使用STL中的容器,得包含它的头文件,set和multiset容器的头文件是一样的,都是set
#include <string>
#include <algorithm>  //使用STL提供的算法,得包含它的头文件

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 MyComparePerson  //制定排序规则
{
public:
	bool operator()(const Person & p1,const Person & p2)  //利用仿函数(重载()运算符)的技术,实现默认排序规则的修改
	{
		//按照年龄的降序进行排序 
		return p1.m_Age > p2.m_Age;
	}
};

void printPersonSet(const set<Person,MyComparePerson> & s)
{
	for(set<Person,MyComparePerson>::const_iterator it=s.begin();it!=s.end();it++)
	{
		cout << "姓名:" << it->m_Name << " 年龄:" << it->m_Age << endl; 
	}
}

void test_1()  //set容器修改排序规则(对自定义数据类型) 
{
	//创建set容器 
	set<Person,MyComparePerson> s;
	
	//创建Person对象
	Person p1("刘备",35);
	Person p2("曹操",45);
	Person p3("孙权",40);
	Person p4("赵云",25);
	Person p5("张飞",29);
	Person p6("关羽",36);
	
	//将Perosn对象插入到set容器中
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	s.insert(p5);
	s.insert(p6);
	
	//遍历打印输出 
	printPersonSet(s);
}

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

以上就是STL中set容器和multiset容器的一些常用接口和用法啦O(∩_∩)O。笔记中有错误的地方,欢迎指出,欢迎大家讨论!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值