C++ set/multiset容器、pair对组

set/multiset容器

简介:所有元素都会在插入时自动被排序
本质:set/multiset属于关联式容器,底层结构是用二叉树实现。
set和multiset区别:set不允许容器中有重复的元素

set构造和赋值

  • 功能描述
  • 创建set容器以及赋值
  • 构造

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

赋值

set& operator=(const set &st);//重载等号操作符

#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 << " ";
	}
	cout << endl;
}
void test1()
{
	set<int> s1;
	//插入数据只有insert方式
	s1.insert(10);
	s1.insert(30);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);
	printSet(s1);//10 20 30 40
	//set容器特点:所有元素在插入时会被自动排序
	//set容器不能插入重复的值(相当于白插)
	set<int> s2(s1);//拷贝构造
	printSet(s2);

	set<int> s3;
	s3 = s2;//赋值
	printSet(s3);
}

int main(void)
{
	test1();
	return 0;
}

总结
1、set容器插入数据时用insert
2、set容器插入的数据会自动排序


set大小和交换

  • 功能描述
    统计set容器大小以及交换set容器
  • 函数原型

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

#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 << " ";
	}
	cout << endl;
}
void test1()
{
	set<int> s1;
	//插入数据只有insert方式
	s1.insert(10);
	s1.insert(30);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);

	set<int> s2;
	s2.insert(100);
	s2.insert(300);
	s2.insert(200);
	
	if (s1.empty())
	{
		cout << "s1为空" << endl;
	}
	else {
		cout << "s1不为空" << endl;
		cout << "s1的大小为:" << s1.size() << endl;//4
	}

	cout << "交换前:" << endl;
	printSet(s1);
	printSet(s2);

	s1.swap(s2);//交换操作
	cout << "交换后:" << endl;
	printSet(s1);
	printSet(s2);
}

int main(void)
{
	test1();
	return 0;
}

总结
统计大小-----size
判断是否为空-----empty
交换容器-----swap


set插入和删除

  • 功能描述
    set容器进行插入数据和删除数据
  • 函数原型

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

#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 << " ";
	}
	cout << endl;
}
void test1()
{
	set<int> s1;
	s1.insert(20);
	s1.insert(10);
	s1.insert(30);
	s1.insert(40);
	printSet(s1);//10 20 30 40

	s1.erase(s1.begin());//(删除头元素)
	printSet(s1);//20 30 40

	s1.erase(30);//(删除指定元素30)删除重载版本
	printSet(s1);//20 40

//	s1.erase(s1.begin(), s1.end());清空
	s1.clear();//清空
	printSet(s1);
}

int main(void)
{
	test1();
	return 0;
}

总结
插入-----insert
删除-----erase
清空-----clear


set查找和统计

  • 功能描述
    对set容器进行查找数据以及统计数据
  • 函数原型

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

#include <iostream>
#include <set>
using namespace std;
void test1()
{
	set<int> s1;
	s1.insert(20);
	s1.insert(10);
	s1.insert(30);
	s1.insert(30);
	s1.insert(40);
	
	set<int>::iterator pos = s1.find(30);//查找
	if (pos != s1.end()) {
		cout << "找到元素:" << *pos << endl;
	}
	else {
		cout << "未找到元素" << endl;
	}
	
	int num = s1.count(30);//对于set而言,统计结果只有1或0两种结果
	cout << "num = " << num << endl;
}

int main(void)
{
	test1();
	return 0;
}

总结
查找-----find(返回的是迭代器)
统计-----count(对于set结果是0或1)


set和multiset区别

  • 区别:
    1、set不可以插入重复数据,而multiset可以
    2、set插入数据的同时会返回插入结果,表示插入是否成功
    3、multiset不会检测数据,因此可以插入重复数据
#include <iostream>
#include <set>
using namespace std;
void test1()
{
	set<int> s1;
	pair<set<int>::iterator, bool> ret = s1.insert(10);
	if (ret.second) {
		cout << "第一次插入成功" << endl;
	}
	else {
		cout << "第一次插入失败" << endl;
	}
	ret = s1.insert(10);
	if (ret.second) {
		cout << "第一次插入成功" << endl;
	}
	else {
		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;
}
int main(void)
{
	test1();
	return 0;
}

总结
如果不允许插入重复数据可以利用set
如果需要插入重复数利用multiset.


pair对组创建

  • 功能描述:
    成对出现的数据,利用对组可以返回两个数据
  • 两种创建方式:

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

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

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

int main(void)
{
	test1();
	return 0;
}

set内置类型指定排序规则

set容器默认排序规则为从小到大

  • 主要技术点:利用仿函数,可以改变排序规则。
#include <iostream>
#include <set>
using namespace std;
class Mysort
{
public:
	bool operator()(int v1,int v2) const
	{
		return v1 > v2;
	}
};
void test1()
{
	set<int> s1;
	s1.insert(20);
	s1.insert(40);
	s1.insert(30);
	s1.insert(10);
	s1.insert(50);

	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;

	set<int,Mysort> s2;
	s2.insert(20);
	s2.insert(40);
	s2.insert(30);
	s2.insert(10);
	s2.insert(50);

	for (set<int,Mysort>::iterator it = s2.begin(); it != s2.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

int main(void)
{
	test1();
	return 0;
}

总结利用仿函数可以指定set容器的排序规则 。


set存放内置数据类型

#include <iostream>
#include <set>
#include <string>
using namespace std;
class Person
{
public:
	Person(string name, int age) {
		this->m_age = age;
		this->m_name = name;
	}
	int m_age;
	string m_name;
};
class Mysort//按照年龄降序排列
{
public:
	bool operator()(const Person &p1,const Person &p2) const//要加const
	{
		return p1.m_age > p2.m_age;
	}
};
void test1()
{
	set<Person,Mysort> s1;

	Person p1("刘备", 25);
	Person p2("张飞", 22);
	Person p3("关羽", 21);
	Person p4("赵云", 27);

	s1.insert(p1);
	s1.insert(p2);
	s1.insert(p3);
	s1.insert(p4);

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

}
int main(void)
{
	test1();
	return 0;
}

总结对于自定义数据类型,set必须指定排序规则才可以插入数据

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值