C++提高编程--STL常用容器(set/multiset、map/multimap容器)详解
·
C++提高编程(五)
1 STL常用容器
1.1 set/multiset容器
1.1.1 set容器基本概念
简介:集合容器,也叫关联式容器,所有元素都会在插入时自动排序
本质:set/multiset属于关联式容器,底层结构是用二叉树实现
set和multiset区别:
- set不允许容器中有重复的元素
- multiset允许容器有重复的元素
1.1.2 set构造和赋值
功能描述:创建set容器以及赋值
构造:
- setst; //默认构造函数
- set(const set&st); //拷贝构造函数
赋值:
set& operator=(const set&st); //重载等号操作符
#include <iostream>
#include<set>
using namespace std;
//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(99);
s1.insert(66);
s1.insert(88);
s1.insert(66);
//遍历容器
//set容器的特点:所有元素插入时会被自动排序
//set容器不允许插入重复值
printSet(s1);
//拷贝构造
set<int>s2(s1);
printSet(s2);
//赋值
set<int>s3;
s3 = s2;
printSet(s3);
}
int main() {
test01();
system("pause");
return 0;
}
输出结果为:

1.1.3 set容器大小和交换
功能描述:统计set容器大小以及交换set容器
函数原型:
- size(); //返回容器中元素的数目
- empty(); //判断容器是否为空
- swap(st); //交换两个集合容器
#include <iostream>
#include<set>
using namespace std;
//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(88);
s1.insert(68);
s1.insert(66);
s1.insert(86);
//打印容器
printSet(s1);
//判断是否为空
if (s1.empty()) {
cout << "s1为空" << endl;
}
else {
cout << "s1不为空" << endl;
cout << "元素个数为:" << s1.size() << endl;
}
}
//交换
void test02() {
set<int>s1;
s1.insert(88);
s1.insert(68);
s1.insert(66);
s1.insert(86);
set<int>s2;
s2.insert(8);
s2.insert(6);
s2.insert(666);
s2.insert(866);
cout << "交换前:" << endl;
//打印容器
printSet(s1);
printSet(s2);
cout << "交换后:" << endl;
s1.swap(s2);
printSet(s1);
printSet(s2);
}
int main() {
test01();
test02();
system("pause");
return 0;
}
输出结果为:

1.1.4 set插入和删除
功能描述:set容器进行插入数据和删除数据
函数原型:
- insert(elem); //在容器中插入元素
- clear(); //清除所有元素
- erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
- erase(beg,end); //删除区间[beg,end)的所有元素,返回下一个元素的迭代器
- erase(elem); //删除容器中值为elem的元素
#include <iostream>
#include<set>
using namespace std;
//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(99);
s1.insert(89);
s1.insert(98);
s1.insert(88);
//遍历
printSet(s1);
//删除
s1.erase(s1.begin());
printSet(s1);
//删除重载版本
s1.erase(89);
printSet(s1);
//清空
s1.erase(s1.begin(),s1.end());
//s1.clear();
printSet(s1);
}
int main() {
test01();
system("pause");
return 0;
}
输出结果为:

1.1.5 set查找和统计
功能描述:对set容器进行查找数据以及统计数据
函数原型:
- find(key); //查找key是否存在,若存在返回该元素的迭代器,不存在返回set.end();
- count(key); //统计key的元素个数
#include <iostream>
#include<set>
using namespace std;
//set容器 查找和统计
void test01() {
//查找
set<int>s1;
//插入数据
s1.insert(88);
s1.insert(89);
s1.insert(98);
s1.insert(99);
//查找
set<int>::iterator pos= s1.find(88);//用一个迭代器去接收查找到的结果
if (pos != s1.end()) {
cout << "找到元素" << *pos<<endl;
}
else {
cout << "未找到元素" << endl;
}
}
//统计
void test02() {
//查找
set<int>s1;
//插入数据
s1.insert(88);
s1.insert(89);
s1.insert(98);
s1.insert(99);
s1.insert(88);
s1.insert(88);
//统计88的个数
//对于set而言,统计的结果要么是0,要么是1,因为它不允许容器中有重复的数
int num = s1.count(88);
cout << "num=" << num << endl;
}
int main() {
test01();
test02();
system("pause");
return 0;
}
输出结果为:

1.1.6 set和multiset的区别
区别:
- set不可以插入重复数据,而multiset可以
- set插入数据的同时会返回插入结果,判断插入是否成功
- multiset不会检测数据,因此可以插入重复数据
#include <iostream>
#include<set>
using namespace std;
//set容器和multiset容器的区别
void test01() {
set<int>s;
//set会返回一个对组的类型,同时还有一个bool值判断它的返回结果成功还是失败
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<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();
system("pause");
return 0;
}
输出结果为:

1.1.7 pair对组的创建
功能描述:成对出现的数据,利用对组可以返回两个数据
两对创建方式:
- pair<type,type>p(value1,value2);
- pair<type,type>p=make_pair(value1,value2);
#include <iostream>
#include<string>
using namespace std;
//pair对组的创建
void test01() {
//第一种方式
pair<string, int>p("Tom", 88);
cout << "姓名:" << p.first << "年龄:" << p.second << endl;
//第二种方式
pair<string, int>p2 = make_pair("yzx", 22);
cout << "姓名:" << p2.first << "年龄:" << p2.second << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出结果为:

1.1.8 set容器排序
学习目标:
- set容器默认排序规则为从小到大,掌握如何改变排序规则
主要技术点:
- 利用仿函数,可以改变排序规则
示例1:set存放内置数据类型
#include <iostream>
#include<set>
using namespace std;
//set容器排序
//仿函数
class MyCompare {
public:
bool operator()(int v1, int v2)const{
return v1 > v2;
}
};
//list排序中的降序操作
//bool MyCompare(int v1, int v2) {
// //降序,就让第一个数>第二个数
// return v1 > v2;
//}
void test01() {
set<int>s1;
s1.insert(88);
s1.insert(98);
s1.insert(89);
s1.insert(898);
s1.insert(988);
//打印数据
for (set<int>::iterator it = s1.begin(); it != s1.end(); it++) {
cout << *it << " ";
}
cout << endl;
//指定排序规则为从大到小
set<int,MyCompare>s2;
s2.insert(88);
s2.insert(98);
s2.insert(89);
s2.insert(898);
s2.insert(988);
for (set<int,MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出结果为:

示例2:set存放自定义数据类型·
#include <iostream>
#include<set>
#include<string>
using namespace std;
//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("刘备", 88);
Person p2("张飞", 68);
Person p3("孙尚香", 18);
Person p4("貂蝉", 28);
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->m_Name << "年龄:" << it->m_Age << endl;
}
}
int main() {
test01();
system("pause");
return 0;
}
输出结果为:

1.2map/multimap容器
1.2.1 map基本概念
简介:
- map中所有元素都是pair
- pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
- 所有元素都会根据元素的键值自动排序
本质:
- map/multimap属于关联式容器,底层结构是用二叉树实现
优点:
- 可以根据key值快速找到value值
map和multimap区别:
- map不允许容器中有重复key值元素
- multimap允许容器中有重复key值元素
1.2.2 map构造和赋值
功能描述:对map容器进行构造和赋值操作
函数原型:
构造:
- map<T1,T2>mp; //map默认构造函数
- map(const map&mp); //拷贝构造函数
赋值:
- map& operator=(const map&mp); //重载等号操作符
#include <iostream>
#include<map>
using namespace std;
//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容器
map<int, int>m;
//插入值
m.insert(pair<int, int>(1, 66));
m.insert(pair<int, int>(4, 686));
m.insert(pair<int, int>(3, 6886));
m.insert(pair<int, int>(2,8888));
printMap(m);
//拷贝构造
map<int, int>m2(m);
printMap(m2);
//赋值
map<int, int>m3;
m3 = m2;
printMap(m3);
}
int main() {
test01();
system("pause");
return 0;
}
输出结果为:

1.2.3 map大小和交换
功能描述:统计map容器大小以及交换map容器
函数原型:
- size(); //返回容器中元素的数目
- empty(); //判断容器是否为空
- swap(st); //交换两个集合容器
#include <iostream>
#include<map>
using namespace std;
//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, 99));
m.insert(pair<int, int>(4, 999));
m.insert(pair<int, int>(2, 899));
m.insert(pair<int, int>(3, 499));
if (m.empty()) {
cout << "m为空" << endl;
}
else {
cout << "m不为空" << endl;
cout << "m的大小为:" << m.size() << endl;
}
}
//交换
void test02() {
map<int, int>m;
m.insert(pair<int, int>(1, 99));
m.insert(pair<int, int>(4, 999));
m.insert(pair<int, int>(2, 899));
m.insert(pair<int, int>(3, 499));
map<int, int>m2;
m2.insert(pair<int, int>(7, 88));
m2.insert(pair<int, int>(6, 988));
m2.insert(pair<int, int>(8, 889));
m2.insert(pair<int, int>(5, 489));
cout << "交换前:" << endl;
printMap(m);
printMap(m2);
m.swap(m2);
cout << "交换后:" << endl;
printMap(m);
printMap(m2);
}
int main() {
test01();
test02();
system("pause");
return 0;
}
输出结果为:

1.2.4 map插入和删除
功能描述:map容器进行插入数据和删除数据
函数原型:
- insert(elem); //在容器中插入元素
- clear(); //清除所有元素
- erase(pos); //删除pos迭代器所指的元素,返回下一个元素的迭代器
- erase(beg,end); //删除区间[beg,end)的所有元素,返回下一个元素的迭代器
- erase(key); //删除容器中值为key的元素
#include <iostream>
#include<map>
using namespace std;
//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, 88));
//第二种
m.insert(make_pair(2, 66));
//第三种
m.insert(map<int, int>::value_type(3, 666));
//第四种
m[4] = 888;
//[]不建议插入,用途,可以利用key访问value
cout <<m[4] << endl;//40
cout << m[5] << endl;//没有5会自动创建一个默认值为0的value
printMap(m);
//删除
m.erase(m.begin());//删除第一个元素
printMap(m);
m.erase(3);//按照key删除,与value无关
printMap(m);
//清空
m.erase(m.begin(), m.end());
//m.clear();
printMap(m);
}
int main() {
test01();
system("pause");
return 0;
}
输出结果为:

1.2.5 map查找和统计
功能描述:对map容器进行查找数据以及统计数据
函数原型:
- find(key); //查找key是否存在,若存在返回该键的元素的迭代器,若不存在返回set.end();
- coint(key); //统计key的元素个数
#include <iostream>
#include<map>
using namespace std;
//map容器 查找和统计
void test01() {
//查找
map<int, int>m;
m.insert(pair<int,int>(1, 88));
m.insert(pair<int,int>(3, 66));
m.insert(pair<int,int>(2, 868));
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
//multimap的count统计可能大于1
int num= m.count(3);
cout << "num=" << num << endl;
}
int main() {
test01();
system("pause");
return 0;
}
输出结果为:

1.2.6 map容器排序
默认规则是按照key从小到大排序,利用仿函数,可以改变排序规则
#include <iostream>
#include<map>
using namespace std;
class MyCompare {
public:
bool operator()(int v1, int v2)const {
//降序
return v1 > v2;
}
};
//map容器 排序
void test01() {
map<int, int,MyCompare>m;
m.insert(pair<int,int>(1, 88));
m.insert(pair<int,int>(3, 66));
m.insert(pair<int,int>(2, 868));
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();
system("pause");
return 0;
}
输出结果为:

更多推荐


所有评论(0)