C/C++提高编程五(set容器、map容器)

set容器构造和赋值

特点: 所有元素都会在插入时自动被排序。

#include <set>
//set容器构造和赋值  二叉树

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

void test01()
{
	set<int>s1;

	//插入数据 只有insert方式
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);
	s1.insert(30);

	//遍历容器
	//set容器特点,所有元素插入时候自动被排序
	//setr容器不允许插入重复值
	printSet(s1);

	//拷贝构造
	set<int>s2(s1);
	printSet(s2);

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

int main()
{
	test01();
}

set大小和交换

  • size(); //返回容器中元素的数目
  • empty(); //判断容器是否为空
  • swap(st); //交换两个集合容器
#include <set>
//set容器 大小和交换

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

//大小
void test01()
{
	set<int>s1;

	//插入数据
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);
	
	//打印容器
	printSet(s1);

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

//交换
void test02()
{
	set<int>s1;
	//插入数据
	s1.insert(10);
	s1.insert(40);
	s1.insert(30);
	s1.insert(20);

	set<int>s2;
	//插入数据
	s2.insert(100);
	s2.insert(300);
	s2.insert(200);
	s2.insert(400);

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

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

int main()
{
	//test01();
	test02();
}

set插入和删除

#include <set>

//set容器 插入和删除
void printSet(set<int>& s)
{
	for (set<int>::iterator it = s.begin(); it != s.end(); it++){
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{
	set<int>s1;

	//插入
	s1.insert(30);
	s1.insert(20);
	s1.insert(40);
	s1.insert(10);
	printSet(s1);

	//删除
	s1.erase(s1.begin());
	printSet(s1);

	//删除指定元素
	s1.erase(30);
	printSet(s1);

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

int main()
{
	test01();
}

set查找和统计

函数原型:

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

//set容器 查找和统计
void test01()
{
	//查找
	set<int>s1;

	//插入数据
	s1.insert(30);
	s1.insert(10);
	s1.insert(40);
	s1.insert(20);

	set<int>::iterator pos = s1.find(30); //返回为该值的迭代器位置
	if (pos != s1.end()){
		cout << "找到元素:" << *pos << endl;
	}
	else{
		cout << "未找到元素" << endl;
	}
}

//统计
void test02()
{
	//查找
	set<int>s1;

	//插入数据
	s1.insert(30);
	s1.insert(10);
	s1.insert(40);
	s1.insert(20);

	//统计30的个数
	int num = s1.count(30);
	//对于set而言 统计结果 为0或1
	cout << "num = " << num << endl;
}

int main()
{
	test01();
	//test02();
}

set和multiset区别

区别:

  • set不可以插入重复数据,而multiset可以
  • set插入数据的同时会返回插入结果,表示插入是否成功
  • multiset不会检测数据,因此可以插入重复数据
#include <set>

//set和multiset区别
void test01()
{
	set<int> s;
	pair<set<int>::iterator, bool>  ret = s.insert(10);
	if (ret.second) {
		cout << "第一次插入成功!" << endl;
	}
	else {
		cout << "第一次插入失败!" << endl;
	}

	ret = s.insert(10);
	if (ret.second) {
		cout << "第二次插入成功!" << endl;
	}
	else {
		cout << "第二次插入失败!" << endl;
	}
    
	//multiset容器
	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() {
	test01();
}

pair对组创建

//pair队组的创建
void test01()
{
	//第一种方式
	pair<string, int> p("Tom", 20);
	cout << "姓名:" << p.first << " 年龄:" << p.second << endl;

	//第二种方式
	pair<string, int>p2 = make_pair("Mark", 30);
	cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}

int main()
{
	test01();
}

set容器排序

学习目标:set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术点:利用仿函数,可以改变排序规则

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

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

//set容器排序
//一个返回值类型为bool类型的仿函数
//仿函数其实本质上就是一个重载了函数调用的运算符operator()函数的一个类类型
class MyCompare
{
public:
	bool operator()(int v1, int v2) const{
		return v1 > v2;
	}
};

//需要一个类
//bool MyCompare(int v1, int v2) 
//{
//	return v1 > v2;
//}

void test01()
{
	set<int>s1;

	s1.insert(10);
	s1.insert(20);
	s1.insert(30);
	s1.insert(40);
	s1.insert(50);

	//默认输出从小到大
	for (set<int>::iterator it = s1.begin(); it != s1.end(); it++){
		cout << *it << " ";
	}
	cout << endl;

	//利用仿函数
	//指定排序规则为从大到小 插数据之前改变其内部的排序规则
	set<int, MyCompare>s2;

	s2.insert(10);
	s2.insert(20);
	s2.insert(30);
	s2.insert(40);
	s2.insert(50);

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

int main()
{
	test01();
}

示例二 set存放自定义数据类型

#include <set>

//set容器 存放自定义数据类型
class Person 
{
public:
	Person(string name, int age){
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};

//仿函数
class comparePerson
{
public:
	bool operator()(const Person&p1, const Person& p2) const{	
		//按照年龄 降序
		return p1.m_Age > p2.m_Age;
	}
};

void test01()
{
	//自定义数据类型 都会指定排序规则
	set<Person, comparePerson>s;
	
	//创建Person对象
	Person p1("刘备", 24);
	Person p2("关羽", 25);
	Person p3("张飞", 26);
	Person p4("赵云", 27);

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

	for (set<Person>::iterator it = s.begin(); it != s.end(); it++){
		cout << "姓名:" << (*it).m_Name << " 年龄:" << (*it).m_Age << endl;
	}
}

int main()
{
	test01();
}

map容器构造和赋值

  • map中所有元素都是pair。
  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)。
  • 所有元素都会根据元素的键值自动排序。
    优点:可以根据key值快速找到value值。
#include <map>

//map容器 构造和赋值  二叉树
//所有元素都是pair
void printMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++){
		cout << "key = " << (*it).first << " value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	//创建map容器
	map<int, int> m;

	//pair第一个元素为key值,起到索引的作用
	//所有元素都会根据元素的键值自动排序
	//第二个元素为value值
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(4, 40));
	m.insert(pair<int, int>(3, 30));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(5, 50));

	printMap(m);

	//拷贝构造
	map<int, int>m2(m);
	printMap(m2);

	//赋值
	map<int, int>m3;
	m3 = m2;
	printMap(m3);
}

int main()
{
	test01();
}

map大小和交换

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

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

//map容器 大小和交换
//大小
void test01()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

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

void printMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++){
		cout << "key = " << (*it).first << " value = " << it->second << endl;
	}
	cout << endl;
}

//交换
void test02()
{
	map<int, int> m;
	m.insert(pair<int, int>(1, 10));
	m.insert(pair<int, int>(2, 20));
	m.insert(pair<int, int>(3, 30));

	map<int, int> m2;
	m2.insert(pair<int, int>(4, 100));
	m2.insert(pair<int, int>(5, 200));
	m2.insert(pair<int, int>(6, 300));

	cout << "交换前:" << endl;
	printMap(m);
	printMap(m2);

	m.swap(m2);
	cout << "交换后:" << endl;
	printMap(m);
	printMap(m2);
}

int main()
{
	//test01();
	test02();
}

map插入和删除

#include <map>

//map容器  插入和删除
void printMap(map<int, int>& m)
{
	for (map<int, int>::iterator it = m.begin(); it != m.end(); it++){
		cout << "key = " << (*it).first << " value = " << it->second << endl;
	}
	cout << endl;
}

void test01()
{
	map<int, int>m;
	//插入
	//第一种
	m.insert(pair<int, int>(1, 10));

	//第二种
	m.insert(make_pair(2, 20));

	//第三种
	m.insert(map<int, int>::value_type(3, 30));

	//第四种
	m[4] = 40;

	//不建议使用[]插入,用途 可以利用key访问value
	cout << m[5] << endl;
	printMap(m); 

	//删除
	m.erase(m.begin());
	printMap(m);

	m.erase(3); //按照key删除
	printMap(m);

	//清空
	//m.erase(m.begin(), m.end());
	m.clear();
	printMap(m);
}

int main()
{
	test01();
}

map查找和统计

#include <map>

//map容器 查找和统计
void test01()
{
	//查找
	map<int, int>m;
	m.insert(pair<int, int>(1, 10));
	m.insert(make_pair(2, 20));
	m.insert(pair<int, int>(3, 30));

	map<int, int>::iterator pos = m.find(3); //返回值为迭代器
	if (pos != m.end()){
		cout << "查到了元素 key = " << (*pos).first << " value = "
			<< pos->second << endl;
	}
	else{
		cout << "未找到元素" << endl;
	}

	//统计
	//map容器不允许插入重复的key 元素 count统计而言 结果0或1
	int num = m.count(3);
	cout << "num = " << num << endl;
}

int main()
{
	test01();
}

map容器排序

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

class MyCompare  //仿函数
{
public:
	bool operator()(int v1, int v2) const{
		return v1 > v2;		//降序
	}
};

//map容器 排序
void test01()
{
	map<int, int, MyCompare>m;   //按照key值进行排序
	m.insert(make_pair(1, 50));
	m.insert(make_pair(2, 40));
	m.insert(make_pair(5, 30));
	m.insert(make_pair(3, 20));
	m.insert(make_pair(4, 10));
	
	for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++){
		cout << "key = " << (*it).first << " value = " << it->second << endl;
	}
	cout << endl;
}

int main()
{
	test01();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值