C++ STL常用容器7、8——set & map容器
本文介绍C++ set/multiset、map/multimap关联式容器,说明自动排序、键值唯一性等特性,讲解增删、查找、自定义排序及pair用法,其底层为二叉树,查找效率较高。
写在前面:⭐如果本篇博文对你有帮助,那就 点赞 + 关注 + 收藏 一下吧!
目录
1.set / multiset 容器
1.1set基本概念
简介:
●所有元素都会在插入时自动被排序
本质:
●set/multiset属于关联式容器,底层结构是用二叉树实现。
set和multiset区别:
●set不允许容器中有重复的元素
●multiset允许容器中有重复的元素
1.2set构造和赋值
功能描述:创建set容器以及赋值

示例:
#include <iostream>
using namespace std;
#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容器特点:所有元素插入时候自动被排序
//set容器不允许插入重复的值
printSet(s1); //10 20 30 40
//拷贝构造
set<int>s2(s1);
printSet(s2);
//赋值
set<int>s3;
s3 = s2;
printSet(s3);
}
int main()
{
test01();
return 0;
}
总结:
●set容器插入数据时用insert
●set容器插入的数据会自动排序●set容器不允许插入重复的值
1.3set大小和交换
功能描述:
●统计set容器大小以及交换set容器
函数原型:
●size( );; //返回容器中元素的数目
●empty( ); //判断容器是否为空
●swap(st) ; //交换两个集合容器
示例:
#include <iostream>
using namespace std;
#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(20);
s1.insert(30);
s1.insert(40);
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(20);
s1.insert(30);
s1.insert(40);
set<int>s2;
s2.insert(100);
s2.insert(200);
s2.insert(300);
s2.insert(400);
cout << "交换前:" << endl;
cout << "s1:";
printSet(s1);
cout << "s2:";
printSet(s2);
s1.swap(s2);
cout << "交换后:" << endl;
cout << "s1:";
printSet(s1);
cout << "s2:";
printSet(s2);
}
int main()
{
test01();
test02();
return 0;
}
/*
10 20 30 40
s1不为空
s1的大小为:4
交换前:
s1:10 20 30 40
s2:100 200 300 400
交换后:
s1:100 200 300 400
s2:10 20 30 40
*/
总结:
●统计大小 —— size
●判断是否为空 —— empty
●交换容器 —— swap
1.4set插入和删除
功能描述:
●set容器进行插入数据和删除数据

示例:
#include <iostream>
using namespace std;
#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(20);
s1.insert(30);
s1.insert(40);
printSet(s1);
//删除
s1.erase(s1.begin());
printSet(s1); //20 30 40
//删除重载版本
s1.erase(30);
printSet(s1);
//清空
//s1.erase(s1.begin(),s1.end());
s1.clear();
printSet(s1);
}
int main()
{
test01();
return 0;
}
1.5set查找和统计
功能描述:
●对set容器进行查找数据以及统计数据
函数原型:
● find(key); //查找key是否存在若存在,返回该键的元素的迭代器;若不存在,返回set.end0:
● count(key); //统计key的元素个数
示例:
#include <iostream>
using namespace std;
#include <set>
//set查找和统计
//查找
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
s1.insert(30);
s1.insert(30);
//查找
set<int>::iterator pos = s1.find(20);
if (pos != s1.end())
{
cout << "找到元素:" << *pos << endl;
}else
{
cout << "未找到元素" << endl;
}
}
//统计
void test02()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
int num = s1.count(30);
//对于set而言,统计结果要么是1要么是0
cout << "30出现的次数为:" << num << endl; //1
}
int main()
{
test01();
test02();
return 0;
}
总结:
● 查找 —— find(返回的是迭代器)
●统计 —— count(对于set,结果为0或者1)
1.6set和multiset区别
学习目标:
●掌握set和multiset的区别
区别:
●set不可以插入重复数据,而multiset可以
●set插入数据的同时会返回插入结果,表示插入是否成功
●multiset不会检测数据,因此可以插入重复数据
示例:
#include <iostream>
using namespace std;
#include <set>
//set 和 multiset 区别
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;
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()
{
test01();
return 0;
}
/*
第一次插入成功
第二次插入失败
10 10
*/
总结:
●如果不允许插入重复数据可以利用set
●如果需要插入重复数据利用multiset
1.7pair对组创建
功能描述:
●成对出现的数据,利用对组可以返回两个数据

示例:
#include <iostream>
using namespace std;
#include <set>
//pair对组创建
void test01()
{
//第一种方式
pair<string,int>p("Tom",20);
cout << "姓名:" << p.first << " 年龄:" << p.second << endl;
//第二种方式
pair<string,int>p2 = make_pair("Jerry",18);
cout << "姓名:" << p2.first << " 年龄:" << p2.second << endl;
}
int main()
{
test01();
return 0;
}
总结:两种方式都可以创建对组,记住一种即可。
1.8set容器排序
学习目标:
●set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术点:
●利用仿函数,可以改变排序规则
示例一 set存放内置数据类型
//set容器排序
//1.set存放内置数据类型
class Compare
{
public:
bool operator()(int v1,int v2) const
{ //比较函数/仿函数必须是 const 成员函数
return v1 > v2;
}
};
void test01()
{
set<int>s1;
s1.insert(10);
s1.insert(30);
s1.insert(20);
s1.insert(40);
s1.insert(50);
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++)
{
cout << *it << " ";
}
cout << endl;
//指定排序规则为从大到小
set<int,Compare>s2;
s2.insert(10);
s2.insert(30);
s2.insert(20);
s2.insert(40);
s2.insert(50);
for (set<int,Compare>::iterator it = s2.begin(); it != s2.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
总结:利用仿函数可以指定set容器的排序规则
示例二 存放自定义数据类型
//2.set存放自定义数据类型
class Person
{
public:
Person(string name,int age){
this->name = name;
this->age = age;
}
string name;
int age;
};
class ComparePerson
{
public:
bool operator()(const Person &p1,const Person &p2) const
{
return p1.age > p2.age;
}
};
void test02()
{
//自定义数据类型都会指定排序规则
set<Person,ComparePerson>s;
Person p1("Tom",10);
Person p2("Jerry",5);
Person p3("Mike",15);
Person p4("Tim",20);
s.insert(p1);
s.insert(p2);
s.insert(p3);
s.insert(p4);
for (set<Person,ComparePerson>::iterator it = s.begin(); it != s.end(); it++)
{
cout << "姓名:" << it ->name << " 年龄:" << it ->age << endl;
}
}
总结:对于自定义数据类型,set必须指定排序规则才可以插入数据
2.map/multimap容器
2.1map基本概念
简介:
●map中所有元素都是pair
●pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
●所有元素都会根据元素的键值自动排序
本质:
●map/multimap属于关联式容器,底层结构是用二叉树实现。
优点:
●可以根据key值快速找到value值
map和multimap区别:
●map不允许容器中有重复key值元素
●multimap允许容器中有重复key值元素
2.2map构造和赋值
功能描述:
●对map容器进行构造和赋值操作
函数原型:
构造:
●map<T1,T2> mp; //map默认构造函数:
●map(const map &mp); //拷贝构造函数
赋值:
●map& operator=(const map &mp); //重载等号操作符
示例:
//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(pair<int,int>(2,20));
m.insert(pair<int,int>(3,30));
m.insert(pair<int,int>(4,40));
printMap(m);
//拷贝构造
map<int,int>m2(m);
printMap(m2);
//赋值
map<int,int>m3;
m3 = m2;
printMap(m3);
}
总结:map中所有元素都是成对出现,插入数据时候要使用对组
2.3map大小和交换
功能描述:
●统计map容器大小以及交换map容器
函数原型:
●size( ); //返回容器中元素的数目
●empty( ); //断容器是否为空
●swap(st); //交换两个集合容器
示例:
//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>m1;
m1.insert(pair<int,int>(1,10));
m1.insert(pair<int,int>(2,20));
m1.insert(pair<int,int>(3,30));
m1.insert(pair<int,int>(4,40));
if (m1.empty())
{
cout << "m1为空" << endl;
}
else
{
cout << "m1不为空" << endl;
cout << "m1的大小为:" << m1.size() << endl;
}
}
//交换
void test02()
{
map<int,int>m1;
m1.insert(pair<int,int>(1,10));
m1.insert(pair<int,int>(2,20));
m1.insert(pair<int,int>(3,30));
m1.insert(pair<int,int>(4,40));
map<int,int>m2;
m2.insert(pair<int,int>(5,50));
m2.insert(pair<int,int>(6,60));
m2.insert(pair<int,int>(7,70));
cout << "交换前:" << endl;
cout << "m1:" << endl;
printMap(m1);
cout << "m2:" << endl;
printMap(m2);
m2.swap(m1);
cout << "交换后:" << endl;
cout << "m1:" << endl;
printMap(m1);
cout << "m2:" << endl;
printMap(m2);
}
总结:
●统计大小 —— size
●判断是否为空 —— empty
●交换容器 —— swap
2.4map插入和删除
功能描述:
●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>m1;
//插入
//第一种
m1.insert(pair<int,int>(1,10));
//第二种
m1.insert(make_pair(2,20));
//第三种
m1.insert(map<int,int>::value_type(3,30));
//第四种
m1[4]=40; //不建议此方法插入,可以利用key访问value
printMap(m1);
//删除
m1.erase(m1.begin());
printMap(m1);
m1.erase(3); //按照key删除
printMap(m1);
m1.erase(m1.begin(),m1.end());
printMap(m1); //清空
}
总结:
●map插入方式很多,记住其一即可
●插入 —— insert
●制除 —— erase
●清空 —— lear
2.5map查找和统计
功能描述:
●对map容器进行查找数据以及统计数据
函数原型:
●find(key); //查找key是否存在若存在,返回该键的元素的迭代器;若不存在,返回set.end0
●count(key); //统计key的元素个数
示例:
//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(pair<int,int>(2,20));
m.insert(pair<int,int>(3,30));
printMap(m);
map<int,int>::iterator ret =m.find(3); //查找key为3的元素,如果找到,返回一个指向该元素的迭代器,否则返回m.end()
if (ret != m.end())
{
cout << "找到了元素" << endl;
cout << "key:" << ret->first << " value:" << ret->second << endl;
}
else
{
cout << "未找到元素" << endl;
}
//统计
//在map容器中,map不允许插入重复key元素,对于count而言,结果要么是0要么是1
//multimap允许插入重复key元素,对于count而言,结果要么是0要么是n
int num = m.count(3);
cout << "num:" << num << endl;
}
总结:
●查找 —— find(返回的是迭代器)
●统计 —— count(对于map,结果为0或者1)
2.6map容器排序
学习目标:
●map容器默认排序规则为按照key值进行从小到大排序,掌握如何改变排序规则
主要技术点:
●利用仿函数,可以改变排序规则
示例:
//map容器排序
class MyCompare
{
public:
bool operator()(int v1,int v2) const
{
//降序
return v1 > v2;
}
};
void test01()
{
map<int,int,MyCompare>m;
m.insert(pair<int,int>(1,10));
m.insert(pair<int,int>(2,20));
m.insert(pair<int,int>(3,30));
m.insert(pair<int,int>(4,40));
m.insert(pair<int,int>(5,50));
for (map<int,int>::iterator it = m.begin(); it != m.end(); it++)
{
cout << "key:" << it ->first << " value:" << it ->second << endl;
}
}
总结:
●利用仿函数可以指定map容器的排序规则
●对于自定义数据类型,map必须要指定排序规则,同set容器
写到后面:⭐如果本篇博文对你有帮助,那就 点赞 + 关注 + 收藏一下吧!
更多推荐


所有评论(0)