黑马c++ STL部分 笔记(9) map/multimap容器

简介: 黑马c++ STL部分 笔记(9) map/multimap容器

map中所有元素都是pair

pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)

所有元素都会根据元素的键值自动排序

本质:

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

优点

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

map和multimap区别

map允许容器中有重复key值元素(value可以重复)

multimap允许容器中有重复key值元素(value可以重复)


1. map容器构造和赋值

// map容器构造和赋值
/*
构造:
map<T1, T2> mp; //map默认构造函数:
map(const map &mp); //拷贝构造函数
赋值:
map& operator=(const map &mp); //重载等号操作符
*/
#include <bits/stdc++.h>
using namespace std;
void printmap1(map<int, int> &m)
{
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
  {
    cout << (*it).first << " " << (*it).second << 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>(4, 40));
  m.insert(pair<int, int>(3, 30));
  printmap1(m); // 10 20 30 40 按key自动升序排序
  // 拷贝构造
  map<int, int> m2(m);
  printmap1(m2); // 10 20 30 40
  //=赋值
  map<int, int> m3;
  m3 = m2;
  printmap1(m3); // 10 20 30 40
}
int main()
{
  test01();
}
/*
总结:
map中所有元素都是成对出现,插入数据时候要使用对组
*/


2. map大小和交换

// map大小和交换
/*
size(); //返回容器中元素的数目
empty(); //判断容器是否为空
swap(st); //交换两个集合容器
*/
#include <bits/stdc++.h>
using namespace std;
void printmap1(map<int, int> &m)
{
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
  {
    cout << (*it).first << " " << (*it).second << 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>(4, 40));
  m.insert(pair<int, int>(3, 30));
  if (m.empty())
  {
    cout << "empty" << endl;
  }
  else
  {
    cout << "not empty" << endl;
    cout << m.size() << endl; // 4
  }
  map<int, int> m2;
  m2.insert(pair<int, int>(12, 10));
  m2.insert(pair<int, int>(24, 20));
  m2.insert(pair<int, int>(41, 40));
  m2.insert(pair<int, int>(35, 30));
  printmap1(m);
  /*1 10
  2 20
  3 30
  4 40*/
  printmap1(m2);
  /*12 10
  24 20
  35 30
  41 40*/
  m.swap(m2);
  printmap1(m);
  /*12 10
  24 20
  35 30
  41 40*/
  printmap1(m2);
  /*1 10
  2 20
  3 30
  4 40*/
}
int main()
{
  test01();
}
/*
总结:
统计大小 — size
判断是否为空 — empty
交换容器 — swap
*/


3. map插入和删除

// map插入和删除
/*
insert(elem); //在容器中插入'元素'。
clear(); //清除所有元素
erase(pos); //删除pos'迭代器'所指的元素,返回下一个元素的迭代器。
erase(beg, end); //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。
erase(key); //删除容器中键值为key的元素。
*/
#include <bits/stdc++.h>
using namespace std;
void printmap1(map<int, int> &m)
{
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
  {
    cout << (*it).first << " " << (*it).second << 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;                                  // 不太建议用,因为m[5]若无值,则输出时输出key=5,value=0
  printmap1(m);                               // 1 10 2 20 3 30 4 40
  cout << endl;
  //[]不建议插数,但可以用[]来用key访问value
  cout << m[5] << endl; // 0
  cout << endl;
  // 删除
  m.erase(m.begin()); // 用pos删除
  printmap1(m);       // 2 20 3 30 4 40 5 0
  cout << endl;
  m.erase(3);   // 按key删除
  printmap1(m); // 2 20 4 40 5 0
  cout << endl;
  m.erase(3);   // 若无key=3,则不删
  printmap1(m); // 2 20 4 40 5 0
  cout << endl;
  m.erase(m.begin(), ++m.begin()); // 可以++,但不能+1/+3
  printmap1(m);                    // 4 40 5 0
  cout << endl;
  m.clear(); // 清空
  printmap1(m);
}
int main()
{
  test01();
}
/*
总结:
map插入方式很多,记住其一即可
插入 — insert
删除 — erase
清空 — clear
*/


4.  map查找和统计

// map查找和统计
/*
find(key); //查找key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回set.end();
count(key); //统计key的元素个数
*/
#include <bits/stdc++.h>
using namespace std;
void printmap1(map<int, int> &m)
{
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
  {
    cout << (*it).first << " " << (*it).second << 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));
  map<int, int>::iterator pos = m.find(3);
  if (pos != m.end())
  {
    cout << "find" << endl;                               // √
    cout << (*pos).first << " " << (*pos).second << endl; // 3 30
  }
  else
  {
    cout << " not find" << endl;
  }
  // 统计  map无重复key
  int num = m.count(3);
  cout << "num=" << num << endl; // 1
}
int main()
{
  test01();
}
/*
总结:
查找 — find (返回的是迭代器)
统计 — count (对于map,结果为0或者1;对于multimap,结果可以>0)
*/


5. map容器排序

5.1内置数据类型的排序

// map容器排序(默认升序)
/*
利用仿函数,可以改变排序规则(改为降序)
*/
#include <bits/stdc++.h>
using namespace std;
class cmp // 仿函数
{
public:
  bool operator()(int k1, int k2)
  {
    return k1 > k2;
  }
};
void test01()
{
  // 查找
  map<int, int> m;
  m.insert(pair<int, int>(1, 10));
  m.insert(pair<int, int>(4, 40));
  m.insert(pair<int, int>(5, 50));
  m.insert(pair<int, int>(2, 20));
  m.insert(pair<int, int>(3, 30));
  for (map<int, int>::iterator it = m.begin(); it != m.end(); it++)
  {
    cout << (*it).first << " " << (*it).second << endl; // 1 10 2 20 3 30 4 40 5 50
  }
  cout << endl;
  map<int, int, cmp> m2;
  m2.insert(pair<int, int>(1, 10));
  m2.insert(pair<int, int>(4, 40));
  m2.insert(pair<int, int>(5, 50));
  m2.insert(pair<int, int>(2, 20));
  m2.insert(pair<int, int>(3, 30));
  for (map<int, int>::iterator it = m2.begin(); it != m2.end(); it++)
  {
    cout << (*it).first << " " << (*it).second << endl; // 5 50 4 40 3 30 2 20 1 10
  }
}
int main()
{
  test01();
}
/*
总结:
利用仿函数可以指定map容器的排序规则
对于自定义数据类型,map必须要指定排序规则,同set容器
*/


5.2自定义数据类型的排序

// map容器排序(默认升序)
/*
利用仿函数,可以改变排序规则(改为降序)
*/
#include <bits/stdc++.h>
using namespace std;
class person
{
public:
  person(string name, int age)
  {
    this->name = name;
    this->age = age;
  }
  string name;
  int age;
};
class cmp // 仿函数
{
public:
  bool operator()(person k1, person k2)
  {
    return k1.age > k2.age; // 按年龄排序
  }
};
void test01()
{
  // 查找
  map<person, int, cmp> m;
  person p1("A", 11);
  person p2("B", 22);
  person p3("C", 33);
  person p4("D", 44);
  person p5("E", 55);
  m.insert(pair<person, int>(p1, 10));
  m.insert(pair<person, int>(p3, 40));
  m.insert(pair<person, int>(p4, 50));
  m.insert(pair<person, int>(p2, 20));
  m.insert(pair<person, int>(p5, 30));
  for (map<person, int>::iterator it = m.begin(); it != m.end(); it++)
  {
    cout << (*it).first.name << " " << (*it).first.age << " " << (*it).second << endl;
  }
  /*
  E 55 30
  D 44 50
  C 33 40
  B 22 20
  A 11 10
  */
}
int main()
{
  test01();
}
/*
总结:
利用仿函数可以指定map容器的排序规则
对于自定义数据类型,map必须要指定排序规则,同set容器
*/


相关文章
|
3天前
|
编译器 C++
《Effective C++ 改善程序与设计的55个具体做法》 第一章 笔记
《Effective C++ 改善程序与设计的55个具体做法》 第一章 笔记
|
4天前
|
算法 C++
算法笔记:递归(c++实现)
算法笔记:递归(c++实现)
|
20小时前
|
算法 前端开发 Linux
【常用技巧】C++ STL容器操作:6种常用场景算法
STL在Linux C++中使用的非常普遍,掌握并合适的使用各种容器至关重要!
26 10
|
3天前
|
编译器 C++
《Effective C++ 改善程序与设计的55个具体做法》 第二章 构造/析构/赋值运算 笔记
《Effective C++ 改善程序与设计的55个具体做法》 第二章 构造/析构/赋值运算 笔记
|
3天前
|
存储 小程序 程序员
Essential C++ 第1章 C++编程基础 (笔记)
Essential C++ 第1章 C++编程基础 (笔记)
|
4天前
|
算法 C语言 C++
面向考试编程C++笔记
面向考试编程C++笔记
|
10天前
|
存储 人工智能 C++
map容器在C++中的具体用法以及相关注意点
map容器在C++中的具体用法以及相关注意点
13 1
|
1天前
|
监控 Java 数据安全/隐私保护
性能监控之 JMX 监控 Docker 容器中的 Java 应用
【6月更文挑战9天】性能监控之 JMX 监控 Docker 容器中的 Java 应用
13 1
|
1天前
|
NoSQL 关系型数据库 Redis
Docker的通俗理解和通过宿主机端口访问Redis容器的实例
本文目标:引导初学者入门Docker,理解镜像、容器和宿主机概念,学习常用Docker命令,特别是如何创建并从Redis容器通过宿主机端口访问。 关键点: - Docker核心:镜像(类)、容器(实例)、宿主机(运行环境)。 - `docker pull` 拉取镜像,如 `redis:3.0`。 - `docker run -d --name` 后台运行容器,如 `my-redis`。 - `-p` 参数做端口映射,如 `6379:6379`。 - `docker exec -it` 交互式进入容器,如 `bash` 或执行命令。
|
5天前
|
运维 Ubuntu Docker
深入理解容器化技术:Docker的应用与实践
在这个数字化转型迅速推进的时代,容器化技术为软件开发和部署提供了新的路径。本文将深入探讨Docker技术的基本原理、应用场景以及实际操作,旨在帮助读者全面理解并掌握这一关键技术。
31 2

热门文章

最新文章