C++ STL标准模板库——set和multiset

C++ STL标准模板库——set和multiset

基本概念

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

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

set和multiset的区别:

  • set不允许容器中有重复的元素
  • multiset允许容器中有重复的元素
void myprintf(set<int> &s)
{
	for (set<int>::iterator it=s.begin();it!=s.end();it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}

构造和赋值

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

赋值:操作符 = ;

示例:

void fun()
{
	set<int> s;
	s.insert(1);
	s.insert(4);
	s.insert(3);
	s.insert(2);
	myprintf(s);

	set<int> s1(s);
	myprintf(s1);
	
	set<int> s2;
	s2 = s;
	myprintf(s2);
}

大小和交换

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

示例:

void fun()
{
	set<int> s;
	s.insert(1);
	s.insert(4);
	s.insert(3);
	s.insert(2);
	myprintf(s);

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

	set<int> s2;
	s2.insert(12);
	s2.insert(14);
	s2.insert(23);
	s2.insert(32);
	myprintf(s2);

	cout << "交换后" << endl;
	s.swap(s2);
	myprintf(s);
	myprintf(s2);
}

插入和删除

注意:set插入元素只有insert()方法

  • .insert(pos,elem);      //在迭代器pos位置插入元素elem
  • clear();            //清空容器内所有数据
  • erase(beg,end);      //清除区间[set.begin(),set.end()]内的元素
  • erase(pos);      //清除迭代器pos位置的元素
  • erase(elem);      //删除容器中值为elem的元素

示例:

void fun()
{
	set<int> s;
	s.insert(1);
	s.insert(4);
	s.insert(3);
	s.insert(2);
	myprintf(s);

	s.erase(s.begin());
	s.erase(--s.end());
	s.erase(s.begin(),s.end());
	s.clear();
	s.erase(3);//删除指定元素
	myprintf(s);
}

查找和统计

  • find(elem);      //查找e元素lem是否存在,若存在,返回该元素的迭代器;若不存在,返回set.end();
  • count(elem);      //统计元素elem的个数

示例:

void fun()
{
	set<int> l;
	l.insert(1);
	l.insert(4);
	l.insert(3);
	l.insert(2);
	myprintf(l);

	set<int>::iterator its = l.find(3);//find(ele)找到返回元素迭代器,找不到返回set.end();
	if (its!=l.end())
	{
		cout << "找到元素:"<<*its << endl;
	
	}
	else
	{
		cout << "未找到元素" << endl;
	}
	cout << "找到元素个数为:" << l.count(3) << endl;//在set内set.count()只会返回0或者1,因为set不允许有重复元素
}

pair对组创建

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

两种创建方式:

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

示例:

void fun()
{
	pair<string, int> p1("Tom",18);
	cout << "姓名:" << p1.first << "  年龄:" << p1.second << endl;

	pair<string, int> p2;
	p2 = make_pair("Jarry", 19);
	cout << "姓名:" << p2.first << "  年龄:" << p2.second << endl;
}

容器排序

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

示例一:set存放内置数据类型

#include<iostream>
#include<set>
using namespace std;
class mycompare
{
public:
	bool operator()(int a,int b)//set是什么类型,这参数就是什么类型
	{
		return a > b;//a>b为降序,a<b为升序
	}
};
void mysprint(set<int,mycompare> &s)
{
	for (set<int, mycompare>::iterator it=s.begin();it!=s.end();it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}
void fun()
{
	set<int,mycompare> s;
	s.insert(3);
	s.insert(1);
	s.insert(4); 
	s.insert(2);
	mysprint(s);
}
int main()
{
	fun();
	return 0;
}

示例二: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 mycompare
{
public:
	bool operator()( person a,  person b)
	{
		return a.m_age > b.m_age;
	}
};

void mysprint(set<person, mycompare> &s)
{
	for (set<person, mycompare>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << (*it).m_name << "  " << (*it).m_age << endl;
	}
	
}
void fun()
{
	set<person, mycompare> s;
	person p1("吕布",28);
	person p2("貂蝉",16);
	person p3("小乔",18);
	person p4("周瑜",26);
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	mysprint(s);
}
int main()
{
	fun();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值