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


相关文章
|
9月前
|
缓存 算法 程序员
C++STL底层原理:探秘标准模板库的内部机制
🌟蒋星熠Jaxonic带你深入STL底层:从容器内存管理到红黑树、哈希表,剖析迭代器、算法与分配器核心机制,揭秘C++标准库的高效设计哲学与性能优化实践。
C++STL底层原理:探秘标准模板库的内部机制
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
408 2
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
833 73
|
存储 算法 C++
【c++丨STL】set/multiset的使用
本文深入解析了STL中的`set`和`multiset`容器,二者均为关联式容器,底层基于红黑树实现。`set`支持唯一性元素存储并自动排序,适用于高效查找场景;`multiset`允许重复元素。两者均具备O(logN)的插入、删除与查找复杂度。文章详细介绍了构造函数、迭代器、容量接口、增删操作(如`insert`、`erase`)、查找统计(如`find`、`count`)及`multiset`特有的区间操作(如`lower_bound`、`upper_bound`、`equal_range`)。最后预告了`map`容器的学习,其作为键值对存储的关联式容器,同样基于红黑树,具有高效操作特性。
717 3
|
存储 算法 C++
【c++丨STL】priority_queue(优先级队列)的使用与模拟实现
本文介绍了STL中的容器适配器`priority_queue`(优先级队列)。`priority_queue`根据严格的弱排序标准设计,确保其第一个元素始终是最大元素。它底层使用堆结构实现,支持大堆和小堆,默认为大堆。常用操作包括构造函数、`empty`、`size`、`top`、`push`、`pop`和`swap`等。我们还模拟实现了`priority_queue`,通过仿函数控制堆的类型,并调用封装容器的接口实现功能。最后,感谢大家的支持与关注。
992 1
|
编译器 C++ 开发者
【C++篇】深度解析类与对象(下)
在上一篇博客中,我们学习了C++的基础类与对象概念,包括类的定义、对象的使用和构造函数的作用。在这一篇,我们将深入探讨C++类的一些重要特性,如构造函数的高级用法、类型转换、static成员、友元、内部类、匿名对象,以及对象拷贝优化等。这些内容可以帮助你更好地理解和应用面向对象编程的核心理念,提升代码的健壮性、灵活性和可维护性。
|
编译器 C++ 容器
【c++11】c++11新特性(上)(列表初始化、右值引用和移动语义、类的新默认成员函数、lambda表达式)
C++11为C++带来了革命性变化,引入了列表初始化、右值引用、移动语义、类的新默认成员函数和lambda表达式等特性。列表初始化统一了对象初始化方式,initializer_list简化了容器多元素初始化;右值引用和移动语义优化了资源管理,减少拷贝开销;类新增移动构造和移动赋值函数提升性能;lambda表达式提供匿名函数对象,增强代码简洁性和灵活性。这些特性共同推动了现代C++编程的发展,提升了开发效率与程序性能。
573 12
|
编译器 C语言 C++
类和对象的简述(c++篇)
类和对象的简述(c++篇)
|
人工智能 机器人 编译器
c++模板初阶----函数模板与类模板
class 类模板名private://类内成员声明class Apublic:A(T val):a(val){}private:T a;return 0;运行结果:注意:类模板中的成员函数若是放在类外定义时,需要加模板参数列表。return 0;
303 0
|
存储 编译器 程序员
c++的类(附含explicit关键字,友元,内部类)
本文介绍了C++中类的核心概念与用法,涵盖封装、继承、多态三大特性。重点讲解了类的定义(`class`与`struct`)、访问限定符(`private`、`public`、`protected`)、类的作用域及成员函数的声明与定义分离。同时深入探讨了类的大小计算、`this`指针、默认成员函数(构造函数、析构函数、拷贝构造、赋值重载)以及运算符重载等内容。 文章还详细分析了`explicit`关键字的作用、静态成员(变量与函数)、友元(友元函数与友元类)的概念及其使用场景,并简要介绍了内部类的特性。
480 0

热门文章

最新文章