C++提高编程——STL:list容器、set容器和map容器

本专栏记录C++学习过程包括C++基础以及数据结构和算法,其中第一部分计划时间一个月,主要跟着黑马视频教程,学习路线如下,不定时更新,欢迎关注
当前章节处于:
---------第1阶段-C++基础入门
---------第2阶段实战-通讯录管理系统,
---------第3阶段-C++核心编程,
---------第4阶段实战-基于多态的企业职工系统
=====>第5阶段-C++提高编程
---------第6阶段实战-基于STL泛化编程的演讲比赛
---------第7阶段-C++实战项目机房预约管理系统

文章目录

  • 一、 list容器
    • 1.1 list基本概念
    • 1.2 list构造函数
    • 1.3 list 赋值和交换
    • 1.4 list 大小操作
    • 1.5 list 插入和删除
    • 1.6 list 数据存取
    • 1.7 list 反转和排序
  • 二、set/ multiset 容器
    • 2.1 set基本概念
    • 2.2 set构造和赋值
    • 2.3 set大小和交换
    • 2.4 set插入和删除
    • 2.5 set查找和统计
    • 2.6 set和multiset区别
    • 2.7 pair对组创建
    • 2.8 set容器排序
  • 三、 map/ multimap容器
    • 3.1 map基本概念
    • 3.2 map构造和赋值
    • 3.3 map大小和交换
    • 3.4 map插入和删除
    • 3.5 map查找和统计
    • 3.6 map容器排序

一、 list容器

1.1 list基本概念

功能:将数据进行链式存储

链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的

链表的组成:链表由一系列结点组成

结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域

STL中的链表是一个双向循环链表

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器

list的优点:

  • 采用动态存储分配,不会造成内存浪费和溢出
  • 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素

list的缺点:

  • 链表灵活,但是空间(指针域) 和 时间(遍历)额外耗费较大

List有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。

总结:STL中List和vector是两个最常被使用的容器,各有优缺点

1.2 list构造函数

功能描述:

  • 创建list容器

函数原型:

  • list<T> lst; //list采用采用模板类实现,对象的默认构造形式:
  • list(beg,end); //构造函数将[beg, end)区间中的元素拷贝给本身。
  • list(n,elem); //构造函数将n个elem拷贝给本身。
  • list(const list &lst); //拷贝构造函数。

示例:

#include <list>void printList(const list<int>& L) {for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {cout << *it << " ";}cout << endl;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);printList(L1);list<int>L2(L1.begin(),L1.end());printList(L2);list<int>L3(L2);printList(L3);list<int>L4(10, 1000);printList(L4);
}int main() {test01();system("pause");return 0;
}
10 20 30 40
10 20 30 40
10 20 30 40
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
请按任意键继续. . .

总结:list构造方式同其他几个STL常用容器,熟练掌握即可

1.3 list 赋值和交换

功能描述:

  • 给list容器进行赋值,以及交换list容器

函数原型:

  • assign(beg, end); //将[beg, end)区间中的数据拷贝赋值给本身。
  • assign(n, elem); //将n个elem拷贝赋值给本身。
  • list& operator=(const list &lst); //重载等号操作符
  • swap(lst); //将lst与本身的元素互换。

示例:

#include <list>void printList(const list<int>& L) {for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {cout << *it << " ";}cout << endl;
}//赋值和交换
void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);printList(L1);//赋值list<int>L2;L2 = L1;printList(L2);list<int>L3;L3.assign(L2.begin(), L2.end());printList(L3);list<int>L4;L4.assign(10, 100);printList(L4);}//交换
void test02()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);list<int>L2;L2.assign(10, 100);cout << "交换前: " << endl;printList(L1);printList(L2);cout << endl;L1.swap(L2);cout << "交换后: " << endl;printList(L1);printList(L2);}int main() {//test01();test02();system("pause");return 0;
}
交换前:
10 20 30 40
100 100 100 100 100 100 100 100 100 100交换后:
100 100 100 100 100 100 100 100 100 100
10 20 30 40
请按任意键继续. . .

总结:list赋值和交换操作能够灵活运用即可

1.4 list 大小操作

功能描述:

  • 对list容器的大小进行操作

函数原型:

  • size(); //返回容器中元素的个数

  • empty(); //判断容器是否为空

  • resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。

    ​ //如果容器变短,则末尾超出容器长度的元素被删除。

  • resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。

      ​					    //如果容器变短,则末尾超出容器长度的元素被删除。
    

示例:

#include <list>void printList(const list<int>& L) {for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {cout << *it << " ";}cout << endl;
}//大小操作
void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);if (L1.empty()){cout << "L1为空" << endl;}else{cout << "L1不为空" << endl;cout << "L1的大小为: " << L1.size() << endl;}//重新指定大小L1.resize(10);printList(L1);L1.resize(2);printList(L1);
}int main() {test01();system("pause");return 0;
}
L1不为空
L1的大小为: 4
10 20 30 40 0 0 0 0 0 0
10 20
请按任意键继续. . .

总结:

  • 判断是否为空 — empty
  • 返回元素个数 — size
  • 重新指定个数 — resize

1.5 list 插入和删除

功能描述:

  • 对list容器进行数据的插入和删除

函数原型:

  • push_back(elem);//在容器尾部加入一个元素
  • pop_back();//删除容器中最后一个元素
  • push_front(elem);//在容器开头插入一个元素
  • pop_front();//从容器开头移除第一个元素
  • insert(pos,elem);//在pos位置插elem元素的拷贝,返回新数据的位置。
  • insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值。
  • insert(pos,beg,end);//在pos位置插入[beg,end)区间的数据,无返回值。
  • clear();//移除容器的所有数据
  • erase(beg,end);//删除[beg,end)区间的数据,返回下一个数据的位置。
  • erase(pos);//删除pos位置的数据,返回下一个数据的位置。
  • remove(elem);//删除容器中所有与elem值匹配的元素。

示例:

#include <list>void printList(const list<int>& L) {for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {cout << *it << " ";}cout << endl;
}//插入和删除
void test01()
{list<int> L;//尾插L.push_back(10);L.push_back(20);L.push_back(30);//头插L.push_front(100);L.push_front(200);L.push_front(300);printList(L);//尾删L.pop_back();printList(L);//头删L.pop_front();printList(L);//插入list<int>::iterator it = L.begin();L.insert(++it, 1000);printList(L);//删除it = L.begin();L.erase(++it);printList(L);//移除L.push_back(10000);L.push_back(10000);L.push_back(10000);printList(L);L.remove(10000);printList(L);//清空L.clear();printList(L);
}int main() {test01();system("pause");return 0;
}
300 200 100 10 20 30
300 200 100 10 20
200 100 10 20
200 1000 100 10 20
200 100 10 20
200 100 10 20 10000 10000 10000
200 100 10 20请按任意键继续. . .

总结:

  • 尾插 — push_back
  • 尾删 — pop_back
  • 头插 — push_front
  • 头删 — pop_front
  • 插入 — insert
  • 删除 — erase
  • 移除 — remove
  • 清空 — clear

1.6 list 数据存取

功能描述:

  • 对list容器中数据进行存取

函数原型:

  • front(); //返回第一个元素。
  • back(); //返回最后一个元素。

示例:

#include <list>//数据存取
void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//cout << L1.at(0) << endl;//错误 不支持at访问数据//cout << L1[0] << endl; //错误  不支持[]方式访问数据cout << "第一个元素为: " << L1.front() << endl;cout << "最后一个元素为: " << L1.back() << endl;//list容器的迭代器是双向迭代器,不支持随机访问list<int>::iterator it = L1.begin();//it = it + 1;//错误,不可以跳跃访问,即使是+1
}int main() {test01();system("pause");return 0;
}
第一个元素为: 10
最后一个元素为: 40
请按任意键继续. . .

总结:

  • list容器中不可以通过[]或者at方式访问数据
  • 返回第一个元素 — front
  • 返回最后一个元素 — back

1.7 list 反转和排序

功能描述:

  • 将容器中的元素反转,以及将容器中的数据进行排序

函数原型:

  • reverse(); //反转链表
  • sort(); //链表排序

示例:

void printList(const list<int>& L) {for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {cout << *it << " ";}cout << endl;
}bool myCompare(int val1 , int val2)
{return val1 > val2;
}//反转和排序
void test01()
{list<int> L;L.push_back(90);L.push_back(30);L.push_back(20);L.push_back(70);printList(L);//反转容器的元素L.reverse();printList(L);//排序L.sort(); //默认的排序规则 从小到大printList(L);L.sort(myCompare); //指定规则,从大到小printList(L);
}int main() {test01();system("pause");return 0;
}
90 30 20 70
70 20 30 90
20 30 70 90
90 70 30 20
请按任意键继续. . .

总结:

  • 反转 — reverse
  • 排序 — sort (成员函数)

排序案例

案例描述:将Person自定义数据类型进行排序,Person中属性有姓名、年龄、身高

排序规则:按照年龄进行升序,如果年龄相同按照身高进行降序

#include <iostream>
using namespace std;
#include<list>
// 创建Person类
class Person {
public:Person(string name, int age,int hight) {this->m_name = name;this->m_age = age;this->m_hight = hight;}string m_name;int m_age;int m_hight;
};
// 输出信息
void printdata(const list<Person> lt) {for (list<Person>::const_iterator it = lt.begin(); it != lt.end(); it++) {cout << "姓名:" << (*it).m_name << "  年龄:" << (*it).m_age << "   身高:" << (*it).m_hight << endl;}
}bool mycompare(Person p1, Person p2) {if (p1.m_age == p2.m_age) {return p1.m_hight > p2.m_hight;}return p1.m_age < p2.m_age;
}int main() {// 创建Person对象Person p1("刘备", 35, 175);Person p2("曹操", 45, 180);Person p3("孙权", 40, 170);Person p4("赵云", 25, 190);Person p5("张飞", 35, 160);Person p6("关羽", 35, 200);// 创建list容器list<Person> lt;lt.push_front(p1);lt.push_front(p2);lt.push_front(p3);lt.push_front(p4);lt.push_front(p5);lt.push_front(p6);lt.sort(mycompare);printdata(lt);system("pause");return 0;}
姓名:赵云  年龄:25   身高:190
姓名:关羽  年龄:35   身高:200
姓名:刘备  年龄:35   身高:175
姓名:张飞  年龄:35   身高:160
姓名:孙权  年龄:40   身高:170
姓名:曹操  年龄:45   身高:180
请按任意键继续. . .

总结:

  • 对于自定义数据类型,必须要指定排序规则,否则编译器不知道如何进行排序

  • 高级排序只是在排序规则上再进行一次逻辑规则制定,并不复杂

二、set/ multiset 容器

2.1 set基本概念

简介:

  • 所有元素都会在插入时自动被排序

本质:

  • set/multiset属于关联式容器,底层结构是用二叉树实现。

set和multiset区别

  • set不允许容器中有重复的元素
  • multiset允许容器中有重复的元素

2.2 set构造和赋值

功能描述:创建set容器以及赋值

构造:

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

赋值:

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

示例:

#include <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(30);s1.insert(20);s1.insert(40);printSet(s1);//拷贝构造set<int>s2(s1);printSet(s2);//赋值set<int>s3;s3 = s2;printSet(s3);
}int main() {test01();system("pause");return 0;
}
10 20 30 40
10 20 30 40
10 20 30 40
请按任意键继续. . .

总结:

  • set容器插入数据时用insert
  • set容器插入数据的数据会自动排序

2.3 set大小和交换

功能描述:

  • 统计set容器大小以及交换set容器

函数原型:

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

示例:

#include <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(30);s1.insert(20);s1.insert(40);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(30);s1.insert(20);s1.insert(40);set<int> s2;s2.insert(100);s2.insert(300);s2.insert(200);s2.insert(400);cout << "交换前" << endl;printSet(s1);printSet(s2);cout << endl;cout << "交换后" << endl;s1.swap(s2);printSet(s1);printSet(s2);
}int main() {//test01();test02();system("pause");return 0;
}
交换前
10 20 30 40
100 200 300 400交换后
100 200 300 400
10 20 30 40
请按任意键继续. . .

总结:

  • 统计大小 — size
  • 判断是否为空 — empty
  • 交换容器 — swap

2.4 set插入和删除

功能描述:

  • set容器进行插入数据和删除数据

函数原型:

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

示例:

#include <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(30);s1.insert(20);s1.insert(40);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();system("pause");return 0;
}
10 20 30 40
20 30 40
20 40请按任意键继续. . .

总结:

  • 插入 — insert
  • 删除 — erase
  • 清空 — clear

2.5 set查找和统计

功能描述:

  • 对set容器进行查找数据以及统计数据

函数原型:

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

示例:

#include <set>//查找和统计
void test01()
{set<int> s1;//插入s1.insert(10);s1.insert(30);s1.insert(20);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);cout << "num = " << num << endl;
}int main() {test01();system("pause");return 0;
}
找到了元素 : 30
num = 1
请按任意键继续. . .

总结:

  • 查找 — find (返回的是迭代器)
  • 统计 — count (对于set,结果为0或者1)

2.6 set和multiset区别

学习目标:

  • 掌握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;}//multisetmultiset<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;
}
第一次插入成功!
第二次插入失败!
10 10
请按任意键继续. . .

总结:

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

2.7 pair对组创建

功能描述:

  • 成对出现的数据,利用对组可以返回两个数据

两种创建方式:

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

示例:

#include <string>//对组创建
void test01()
{pair<string, int> p(string("Tom"), 20);cout << "姓名: " <<  p.first << " 年龄: " << p.second << endl;pair<string, int> p2 = make_pair("Jerry", 10);cout << "姓名: " << p2.first << " 年龄: " << p2.second << endl;
}int main() {test01();system("pause");return 0;
}
姓名: Tom 年龄: 20
姓名: Jerry 年龄: 10
请按任意键继续. . .

总结:

两种方式都可以创建对组,记住一种即可

2.8 set容器排序

学习目标:

  • set容器默认排序规则为从小到大,掌握如何改变排序规则

主要技术点:

  • 利用仿函数,可以改变排序规则

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

#include <set>class MyCompare 
{
public:bool operator()(int v1, int v2) {return v1 > v2;}
};
void test01() 
{    set<int> s1;s1.insert(10);s1.insert(40);s1.insert(20);s1.insert(30);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(40);s2.insert(20);s2.insert(30);s2.insert(50);for (set<int, MyCompare>::iterator it = s2.begin(); it != s2.end(); it++) {cout << *it << " ";}cout << endl;
}int main() {test01();system("pause");return 0;
}
10 20 30 40 50
50 40 30 20 10
请按任意键继续. . .

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

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

#include <set>
#include <string>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){//按照年龄进行排序  降序return p1.m_Age > p2.m_Age;}
};void test01()
{set<Person, comparePerson> s;Person p1("刘备", 23);Person p2("关羽", 27);Person p3("张飞", 25);Person p4("赵云", 21);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;
}
姓名:赵云  年龄:25   身高:190
姓名:刘备  年龄:35   身高:175
姓名:孙权  年龄:40   身高:170
姓名:曹操  年龄:45   身高:180
请按任意键继续. . .

总结:

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

三、 map/ multimap容器

3.1 map基本概念

简介:

  • map中所有元素都是pair
  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)
  • 所有元素都会根据元素的键值自动排序

本质:

  • map/multimap属于关联式容器,底层结构是用二叉树实现。

优点:

  • 可以根据key值快速找到value值

map和multimap区别

  • map不允许容器中有重复key值元素
  • multimap允许容器中有重复key值元素

3.2 map构造和赋值

功能描述:

  • 对map容器进行构造和赋值操作

函数原型:

构造:

  • map<T1, T2> mp; //map默认构造函数:
  • map(const map &mp); //拷贝构造函数

赋值:

  • map& operator=(const map &mp); //重载等号操作符

示例:

#include <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>m2(m); //拷贝构造printMap(m2);map<int, int>m3;m3 = m2; //赋值printMap(m3);
}int main() {test01();system("pause");return 0;
}
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30key = 1 value = 10
key = 2 value = 20
key = 3 value = 30key = 1 value = 10
key = 2 value = 20
key = 3 value = 30请按任意键继续. . .

总结:map中所有元素都是成对出现,插入数据时候要使用对组

3.3 map大小和交换

功能描述:

  • 统计map容器大小以及交换map容器

函数原型:

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

示例:

#include <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));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, 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);cout << "交换后" << endl;m.swap(m2);printMap(m);printMap(m2);
}int main() {test01();test02();system("pause");return 0;
}
m不为空
m的大小为: 3
交换前
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30key = 4 value = 100
key = 5 value = 200
key = 6 value = 300交换后
key = 4 value = 100
key = 5 value = 200
key = 6 value = 300key = 1 value = 10
key = 2 value = 20
key = 3 value = 30请按任意键继续. . .

总结:

  • 统计大小 — size
  • 判断是否为空 — empty
  • 交换容器 — swap

3.4 map插入和删除

功能描述:

  • map容器进行插入数据和删除数据

函数原型:

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

示例:

#include <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; printMap(m);//删除m.erase(m.begin());printMap(m);m.erase(3);printMap(m);//清空m.erase(m.begin(),m.end());m.clear();printMap(m);
}int main() {test01();system("pause");return 0;
}
key = 1 value = 10
key = 2 value = 20
key = 3 value = 30
key = 4 value = 40key = 2 value = 20
key = 3 value = 30
key = 4 value = 40key = 2 value = 20
key = 4 value = 40请按任意键继续. . .

总结:

  • map插入方式很多,记住其一即可

  • 插入 — insert

  • 删除 — erase

  • 清空 — clear

3.5 map查找和统计

功能描述:

  • 对map容器进行查找数据以及统计数据

函数原型:

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

示例:

#include <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));//查找map<int, int>::iterator pos = m.find(3);if (pos != m.end()){cout << "找到了元素 key = " << (*pos).first << " value = " << (*pos).second << endl;}else{cout << "未找到元素" << endl;}//统计int num = m.count(3);cout << "num = " << num << endl;
}int main() {test01();system("pause");return 0;
}
找到了元素 key = 3 value = 30
num = 1
请按任意键继续. . .

总结:

  • 查找 — find (返回的是迭代器)
  • 统计 — count (对于map,结果为0或者1)

3.6 map容器排序

学习目标:

  • map容器默认排序规则为 按照key值进行 从小到大排序,掌握如何改变排序规则

主要技术点:

  • 利用仿函数,可以改变排序规则

示例:

#include <map>class MyCompare {
public:bool operator()(int v1, int v2)const {return v1 > v2;}
};void test01() 
{//默认从小到大排序//利用仿函数实现从大到小排序map<int, int, MyCompare> m;m.insert(make_pair(1, 10));m.insert(make_pair(2, 20));m.insert(make_pair(3, 30));m.insert(make_pair(4, 40));m.insert(make_pair(5, 50));for (map<int, int, MyCompare>::iterator it = m.begin(); it != m.end(); it++) {cout << "key:" << it->first << " value:" << it->second << endl;}
}
int main() {test01();system("pause");return 0;
}
key:5 value:50
key:4 value:40
key:3 value:30
key:2 value:20
key:1 value:10
请按任意键继续. . .

总结:

  • 利用仿函数可以指定map容器的排序规则
  • 对于自定义数据类型,map必须要指定排序规则,同set容器

实战案例-员工分组
案例描述

  • 公司今天招聘了10个员工(ABCDEFGHIJ),10名员工进入公司之后,需要指派员工在那个部门工作
  • 员工信息有: 姓名 工资组成;部门分为:策划、美术、研发
  • 随机给10名员工分配部门和工资
  • 通过multimap进行信息的插入 key(部门编号) value(员工)
  • 分部门显示员工信息

实现步骤

  1. 创建10名员工,放到vector中
  2. 遍历vector容器,取出每个员工,进行随机分组
  3. 分组后,将员工部门编号作为key,具体员工作为value,放入到multimap容器中
  4. 分部门显示员工信息

案例代码:

#include <iostream>
using namespace std;
#include <vector>
#include <map>
// 定义员工类
class Person {
public:Person(string name, int money) {this->m_name = name;this->m_money = money;}string m_name;int m_money;
};
// 添加员工到vector中
void addPerson(vector<Person>& v) {// 创建员工string namelist = "ABCDEFGHIJ";for (int i = 0; i < 10; i++) {string name = "员工";name += namelist[i];int money = rand() * 100;Person p(name, money);v.push_back(p); // 添加员工}
}
// 添加员工和部门之间关系
void addinf(multimap<string, Person> &mp, vector<Person>& v) {// 部门mapmap<int, string> infmp;infmp.insert(make_pair(1, "策划"));infmp.insert(make_pair(2, "美术"));infmp.insert(make_pair(3, "研发"));for (int i = 0; i < 10; i++) {// 创建随机部门int num = rand() % 3+1;//cout << infmp[num] << endl;mp.insert(make_pair(infmp[num],v[i]));}
}
void showdata(multimap<string, Person> &mp) {multimap<string, Person>::iterator pos = mp.find("策划");cout << "策划部门:" << endl;int count = mp.count("策划"); // 统计具体人数int index = 0;for (; pos != mp.end() && index < count; pos++, index++){cout << "姓名: " << pos->second.m_name << " 工资: " << pos->second.m_money << endl;}cout << "美术部门:" << endl;count = mp.count("美术"); // 统计具体人数index = 0;for (; pos != mp.end() && index < count; pos++, index++){cout << "姓名: " << pos->second.m_name << " 工资: " << pos->second.m_money << endl;}cout << "研发部门:" << endl;count = mp.count("研发"); // 统计具体人数index = 0;for (; pos != mp.end() && index < count; pos++, index++){cout << "姓名: " << pos->second.m_name << " 工资: " << pos->second.m_money << endl;}
}
int main() {srand((unsigned int)time(NULL));vector<Person> v;addPerson(v);multimap<string, Person> mp;addinf(mp,v);showdata(mp);system("pause");return 0;}
策划部门:
姓名: 员工B 工资: 313100
姓名: 员工C 工资: 1022600
姓名: 员工D 工资: 2403900
姓名: 员工I 工资: 2245700
姓名: 员工J 工资: 2977900
美术部门:
姓名: 员工A 工资: 3176900
姓名: 员工E 工资: 625100
姓名: 员工G 工资: 2297300
研发部门:
姓名: 员工F 工资: 1143300
姓名: 员工H 工资: 1370600
请按任意键继续. . .

总结:

  • 当数据以键值对形式存在,可以考虑用map 或 multimap

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/652081.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

洛谷p1036选数

[NOIP2002 普及组] 选数 题目描述 已知 n n n 个整数 x 1 , x 2 , ⋯ , x n x_1,x_2,\cdots,x_n x1​,x2​,⋯,xn​&#xff0c;以及 1 1 1 个整数 k k k&#xff08; k < n k<n k<n&#xff09;。从 n n n 个整数中任选 k k k 个整数相加&#xff0c;可分别得…

SAP-PP: BOM选择标识不存在

在MM03 中选择生产版本无法打开 原因是未维护 BOM选择标识 配置路径&#xff1a;生产→物料需求计划→工厂参数→执行工厂参数总体维护→BOM/工艺路线选择 T-CD: OPPQ

flutter极光推送配置厂商通道(华为)笔记--进行中

一、基础集成按照下面官方文档进行 厂商通道相关参数申请教程 集成厂商 集成指南 官方文档&#xff1a;厂商通道回执配置指南 注意&#xff1a;不同厂商对app上架的要求不同&#xff0c;华为、荣耀 对app上架没有硬性要求 遇到问题 1、引入apply plugin: com.huawei.agconn…

Jenkins部署及应用

一. 简介 二. 下载地址源&#xff1a; …

Docker部署Stable-Diffusion-webui

前排提示&#xff1a;如果不想折腾&#xff0c;可直接跳到最后获取封装好的容器&#xff0c;一键运行 :D 前言 乘上AI生成的快车&#xff0c;一同看看沿途的风景。 启一个miniconda容器 docker run -itd -v 宿主机内SD项目路径:/tmp --gpus all --ipc host -p 7860:7860 con…

机器学习---可能近似正确(PAC)、出错界限框架

1. 计算学习理论概述 从理论上刻画了若干类型的机器学习问题中的困难和若干类型的机器学习算法的能力 这个理论要回答的问题是&#xff1a; 在什么样的条件下成功的学习是可能的&#xff1f; 在什么条件下某个特定的学习算法可保证成功运行&#xff1f; 这里考虑两种框架&…

《SPSS统计学基础与实证研究应用精解》视频讲解:数据结构重组(数据重组方式的选择、由变量组到样本观测值组的重组、由样本观测值组到变量组的重组)

《SPSS统计学基础与实证研究应用精解》4.11 视频讲解 视频为《SPSS统计学基础与实证研究应用精解》张甜 杨维忠著 清华大学出版社 一书的随书赠送视频讲解4.11节内容。本书已正式出版上市&#xff0c;当当、京东、淘宝等平台热销中&#xff0c;搜索书名即可。本书旨在手把手教会…

2024年阿里云幻兽帕鲁Palworld游戏服务器优惠价格表

自建幻兽帕鲁服务器租用价格表&#xff0c;2024阿里云推出专属幻兽帕鲁Palworld游戏优惠服务器&#xff0c;配置分为4核16G和4核32G服务器&#xff0c;4核16G配置32.25元/1个月、10M带宽66.30元/1个月、4核32G配置113.24元/1个月&#xff0c;4核32G配置3个月339.72元。ECS云服务…

求x大于等于n的最小因子-codeforce round 921 div2 B

CF的round921div2的B题 题目简介与分析 有一堆废话我就不提了&#xff0c;直奔重点&#xff0c;然后是他问你x分成n个数相加的形式&#xff0c;然后要求这n个数的最大公因数最大&#xff0c;并问你最大时这个最大公因数是多少。 我的思路 我把x看成很多个质因数相乘得到&…

gdb调试std::list和std::vector等容器的方法

GDB中print方法并不能直接打印STL容器中保存的变量&#xff0c;其实只要http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt这个文件保存为~/.gdbinit 就可以使用它提供的方法方便调试容器 指定启动文件&#xff1a;~/.gdbinit&#xff0c;下面的方法任选其一。…

2024年华为OD机试真题-围棋的气-Python-OD统一考试(C卷)

题目描述: 围棋棋盘由纵横各19条线垂直相交组成,棋盘上一共19x19=361个交点,对弈双方一方执白棋,一方执黑棋,落子时只能将棋子置于交点上。 “气”是围棋中很重要的一个概念,某个棋子有几口气,是指其上下左右方向四个相邻的交叉点中,有几个交叉点没有棋子,由此可知: …

Debian 12.x apt方式快速部署LNMP

一.前期准备 1.建议服务器以最小化方式安装Debian 12。 何为Debian 12最小化安装呢&#xff1f;就是在安装Debian 12选择软件时只勾选SSH server和standard system utilities即可。另外&#xff0c;在分区时建议分一个/data分区&#xff0c;为服务器的数据目录。 2.配置源 #cd …

ffmpeg4.0.4 api-threadmessage-test.c

使用FFmpeg库模拟消息传递的C程序。它创建了一个消息队列以及多个发送者和接收者线程&#xff0c;这些线程在队列中推送和弹出消息。 程序的主要功能包括&#xff1a; 定义了发送者和接收者数据的结构&#xff0c;以及消息的结构。 free_frame函数用于释放消息帧的内存。 sen…

《动手学深度学习(PyTorch版)》笔记4.9

Chapter4 Multilayer Perceptron 4.9 Environment and Distribution Shift 4.9.1 Types of Distribution Shift 在一个经典的情景中&#xff0c;假设训练数据是从某个分布 p S ( x , y ) p_S(\mathbf{x},y) pS​(x,y)中采样的&#xff0c;但是测试数据将包含从不同分布 p T …

leetcode 字符串相关题目

344. 反转字符串 - 力扣&#xff08;LeetCode&#xff09; 题解&#xff1a;力扣&#xff08;LeetCode&#xff09;官网 - 全球极客挚爱的技术成长平台 541. 反转字符串 II - 力扣&#xff08;LeetCode&#xff09; 题解&#xff1a;https://leetcode.cn/problems/reverse-s…

Linux cat,tac,more,head,tail命令 查看文本

目录 一. cat 和 tac命令二. head 和 tail 命令三. more命令 一. cat 和 tac命令 cat&#xff1a;用来打开文本文件&#xff0c;从上到下的顺序显示文件内容。tac&#xff1a;用法和cat相同&#xff0c;只不过是从下到上逆序的方式显示文件内容。当文件的内容有很多的时候&…

《Python 简易速速上手小册》第6章:Python 文件和数据持久化(基于最新版 Python3.12 编写)

注意&#xff1a;本《Python 简易速速上手小册》 核心目的在于让零基础新手「快速构建 Python 知识体系」 文章目录 <mark >注意&#xff1a;本《Python 简易速速上手小册》<mark >核心目的在于让零基础新手「快速构建 Python 知识体系」 6.1 文件读写操作6.1.1 打…

【leetcode100-069到073】【栈】五题合集

【有效括号】 给定一个只包括 (&#xff0c;)&#xff0c;{&#xff0c;}&#xff0c;[&#xff0c;] 的字符串 s &#xff0c;判断字符串是否有效。 有效字符串需满足&#xff1a; 左括号必须用相同类型的右括号闭合。左括号必须以正确的顺序闭合。每个右括号都有一个对应的…

【Image captioning】论文阅读八—ClipCap: CLIP Prefix for Image Captioning_2021

中文标题&#xff1a;ClipCap: CLIP前缀用于图像描述&#xff08;ClipCap: CLIP Prefix for Image Captioning&#xff09; 文章目录 1. 介绍2. 相关工作3. 方法3.1 综述3.2 语言模型微调3.3 映射网络架构3.4 推理 4. 结果5. 结论 摘要&#xff1a;图像描述是视觉语言理解中的…

函数入门.

函数入门 1. 初识函数2. 函数的参数2.1 参数2.2 默认参数2.3 动态参数 3. 函数返回值总结作业 1. 初识函数 函数到底是个什么东西&#xff1f; 函数&#xff0c;可以当做是一大堆功能代码的集合。 def 函数名():函数内编写代码......函数名()例如&#xff1a; # 定义名字叫in…