set/multiser容器(学习笔记)

1.基本概念:

所有元素的会在插入时自动被排序
set/multiste属于关联式容器,底层是用二叉树实现
区别:set不允许容器中有重复的元素,mulitset允许有重复的元素

2.set的构造与赋值:

构造:
set<T>st;
set(const set &st);
赋值:
set& operator=(const set &st);

3.大小与交换

功能:
统计set容器大小以及交换set容器
函数原型:
size();
empty();
swap(st);

4.插入删除


功能:
set容器插入和删除
函数原型:
insert(elem);
clear();//清除所有元素
erase(pos);//删除pos迭代器所致元素,返回下一个元素的迭代器
erase(beg,end);//删除[beg,end)迭代器所致元素,返回下一个元素的
eream(elem);

5.查找和统计


功能:
set容器查找和统计
函数原型:
find(key);//查找key是否存在,若存在返回该键的元素,若不存在则返回set.end();
count(key);//统计key的个数;因为set中不能有重复的元素,所有是0或1

#include<iostream>
using namespace std;
#include<set>
void print(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}cout << endl;
}

void test01()
{
	//查找
	set<int>s;
	s.insert(10);
	s.insert(30);
	s.insert(20);
	s.insert(40);
	print(s);
	set<int>::iterator pos = s.find(20);
	if (pos != s.end())
	{
		cout << "找到该元素" <<*pos<< endl;
	}
	else
	{
		cout << "未找到该元素" << endl;
	}
	//统计
	int num = s.count(30);
	cout << "该数据的个数为" << num << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

6.对组的创建

#include<iostream>
using namespace std;
#include<string>
//成对出现的数据可以用对组来返回两个数据
void test01()
{
	//方式一
	//创建对组p
	pair<string, int>p("tom", 18);
	//访问p
	cout << "姓名:" << p.first << "年龄:" << p.second << endl;

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

7.改变排序规则


目标:set默认从小到大,改为从大到小
技术点:利用仿函数,(必须重载 operator() 运算符);本质上是一个类型

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

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

//重新定义排序方式
class myCompare
{
public:
	//返回值类型是bool类型,重载小括号()
	bool operator()(int v1,int v2)const
	{
		return v1 > v2;
	}
};
void test01()
{
	set<int>s;
	s.insert(10);
	s.insert(30);
	s.insert(20);
	s.insert(50);
	s.insert(40);
	for (set<int>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout << *it << " ";
	}cout << endl;

	//规定排序从大到小
	set<int, myCompare>s1;
	s1.insert(10);
	s1.insert(30);
	s1.insert(20);
	s1.insert(50);
	s1.insert(40);
	for (set<int,myCompare>::iterator it = s1.begin(); it != s1.end(); it++)
	{
		cout << *it << " ";
	}cout << endl;
}
int main()
{
	test01();
	system("pause");
	return 0;
}

示例二:排序自定义数据类型

#include<iostream>
using namespace std;
#include<set>
//创建一个类
class Person
{
public:
	string m_name; int m_age;
	Person(string name, int age)
	{
		this->m_name = name;
		this->m_age = age;
	}
};
//重新定义排序方式,仿函数
class myCompare
{
public:
	//返回值类型是bool类型,重载小括号()
	bool operator()(const Person &p1, const Person& p2)const
	{
		return p1.m_age > p2.m_age;
	}
};
void test01()
{
	set<Person, myCompare>s;
	Person p1("11", 15);
	Person p2("22", 13);
	Person p3("33", 17);
	Person p4("44", 24);
	Person p5("55", 19);

	//插入
	s.insert(p1);
	s.insert(p2);
	s.insert(p3);
	s.insert(p4);
	s.insert(p5);

	//输出
	for (set<Person, myCompare>::iterator it = s.begin(); it != s.end(); it++)
	{
		cout <<"姓名:"<<it->m_name<<"年龄:"<<it->m_age<< endl;
	}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值