黑马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容器
*/


相关文章
|
11月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
308 2
|
11月前
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
606 73
|
8月前
|
存储 编译器 容器
set、map、multiset、multimap的介绍及使用以及区别,注意事项
set是按照一定次序存储元素的容器,使用set的迭代器遍历set中的元素,可以得到有序序列。set当中存储元素的value都是唯一的,不可以重复,因此可以使用set进行去重。set默认是升序的,但是其内部默认不是按照大于比较,而是按照小于比较。set中的元素不能被修改,因为set在底层是用二叉搜索树来实现的,若是对二叉搜索树当中某个结点的值进行了修改,那么这棵树将不再是二叉搜索树。
306 0
|
存储 C++ 索引
C++基础知识(八:STL标准库 Map和multimap )
C++ 标准模板库(STL)中的 map 容器是一种非常有用的关联容器,用于存储键值对(key-value pairs)。在 map 中,每个元素都由一个键和一个值组成,其中键是唯一的,而值则可以重复。
360 1
|
分布式计算 Python
【python笔记】高阶函数map、filter、reduce
【python笔记】高阶函数map、filter、reduce
377 0
|
存储 算法 C++
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
|
存储 语音技术 Python
语音识别,函数综合案例,黑马ATM,/t/t一个对不齐,用两个/t,数据容器入门,数据容器可以分为列表(list)、元组(tuple)、字符串(str)、集合(set)、字典(dict)
语音识别,函数综合案例,黑马ATM,/t/t一个对不齐,用两个/t,数据容器入门,数据容器可以分为列表(list)、元组(tuple)、字符串(str)、集合(set)、字典(dict)
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
165 0
|
6月前
|
Kubernetes Docker Python
Docker 与 Kubernetes 容器化部署核心技术及企业级应用实践全方案解析
本文详解Docker与Kubernetes容器化技术,涵盖概念原理、环境搭建、镜像构建、应用部署及监控扩展,助你掌握企业级容器化方案,提升应用开发与运维效率。
1023 108