第九层(9):STL之map/multimap

简介: 第九层(9):STL之map/multimap

前情回顾


在上一块石碑中,我学到了set/multiset容器,同时下一块石碑也显露出来…


🚄上章地址:第九层(8):STL之set/multiset


map/multimap


概念


在map/multimap中所有元素都是pair数组,在这个pair数组中,第一个元素是key(键值),起到引索的作用,可以根据key快速定位到对应的value(实值),所有元素是会根据key值来进行排序的,底层与set一样,也是二叉树


差别


map和multimap的差别在于key值的重复插入,在map中同样的key只可以有一个,但是multimap可以有多个


构造函数


map和set的构造函数是基本一样的,但是它的模板参数列表必须要两个参数,第一个代表键值,第二个代表实值

map<T1 ,T2>;//map的默认构造函数
map(vonst map &m);//将m的值拷贝给本身


使用:


#include<iostream>
#include<map>
using namespace std;
void print(map<int ,int>& m)
{
  for (map<int, int>::iterator b = m.begin(); b != m.end(); b++)
  {
  cout << (*b).second<< " ";
  }
  cout << endl;
}
void test1()
{
  map<int, int> m;
  m.insert(pair<int, int>(1, 10));
  m.insert(pair<int, int>(0, 20));
  print(m);
  map<int, int> m1(m);
  print(m1);
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


赋值操作


和set一样,只有操作符重载

map& operator=(const map& m);//将m的值拷贝到本身


使用:


#include<iostream>
#include<map>
using namespace std;
void print(map<int ,int>& m)
{
  for (map<int, int>::iterator b = m.begin(); b != m.end(); b++)
  {
  cout << (*b).second<< " ";
  }
  cout << endl;
}
void test1()
{
  map<int, int> m;
  m.insert(pair<int, int>(1, 10));
  m.insert(pair<int, int>(0, 20));
  print(m);
  map<int, int> m1;
  m1 = m;
  print(m1);
}
int main()
{
  test1();
  return 0;
}

0eacb84100b54626af849e6b562bf92a.png


大小操作函数


map对于大小是不能重新定义的,只能知道有多少个元素,或者是否为空,与set一样

size()://返回容器中元素个数
empty();//判断容器是否为空


使用:


#include<iostream>
#include<map>
using namespace std;
void test1()
{
  map<int, int> m;
  if (m.empty())
  {
  cout << "m为空" << endl;
  }
  cout << m.size() << endl;
}
int main()
{
  test1();
  return 0;
}

2d65d23f6d4748949b924e4057485923.png


交换函数


与set是一样的,都利用swap交换

swap(m);//与m交换数据


使用:


#include<iostream>

#include<map>
using namespace std;
void print(map<int,int>& m)
{
  for (map<int,int>::iterator b = m.begin(); b != m.end(); b++)
  {
  cout << (*b).second << " ";
  }
  cout << endl;
}
void test1()
{
  map<int,int> m;
  m.insert(pair<int,int>(0,10));
  map<int,int> m1;
  m1.insert(pair<int,int>(0,20));
  cout << "交换前" << endl;
  print(m);
  print(m1);
  m.swap(m1);
  cout << "交换后" << endl;
  print(m);
  print(m1);
}
int main()
{
  test1();
  return 0;
}

2e9b90b2ca334476abebe75bafe6eeaa.png


插入函数


map的插入有三种不同的写法

insert(pair<int,T>(key,T elem));//插入键值为key的elem
insert(make_pair(key,T elem));//插入键值为key的elem
insert(map<int ,T>::value_type(key,T elem));//插入键值为key的elem


使用:


#include<iostream>
#include<map>
using namespace std;
void print(map<int,int>& m)
{
  for (map<int,int>::iterator b = m.begin(); b != m.end(); b++)
  {
  cout << (*b).second << " ";
  }
  cout << endl;
}
void test1()
{
  map<int, int> m;
  m.insert(pair<int, int>(0, 10));
  m.insert(pair<int, int>(1, 20));
  print(m);
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


删除函数


同样与set是一样的,具有四个删除函数

clear();//删除容器中所有元素
erase(pos);//删除迭代器pos指向的元素,返回下一个元素的迭代器
erase(beg,end);//删除迭代器beg到end之间的元素,返回下一个元素的迭代器
erase(T elme);//删除容器中所有的实值为elem的元素


使用:


#include<iostream>
#include<map>
using namespace std;
void print(map<int,int>& m)
{
  for (map<int,int>::iterator b = m.begin(); b != m.end(); b++)
  {
  cout << (*b).second << " ";
  }
  cout << endl;
}
void test1()
{
  map<int,int> m;
  for (int i = 0; i < 10; i++)
  {
  m.insert(pair<int,int>(i,i+10));
  m.insert(pair<int, int>(i+10, i));
  }
  print(m);
  map<int,int>::iterator b = m.begin();
  b++; b++; b++; b++; b++;
  m.erase(b);
  print(m);
  map<int,int>::iterator b1 = m.begin();
  b1++; b1++; b1++; b1++; b1++;
  m.erase(b1, m.end());
  print(m);
  m.erase(0);
  print(m);
  m.clear();
  print(m);
}
int main()
{
  test1();
  return 0;
}

0eacb84100b54626af849e6b562bf92a.png


查找函数


同set

find(key);//查找key是否存在,存在返回该键值所指元素的迭代器,不存在返回end()


使用:


#include<iostream>
#include<map>
using namespace std;
void test1()
{
  map<int,int> m;
  for (int i = 0; i < 10; i++)
  {
  m.insert(make_pair(i,i+10));
  }
  map<int,int>::iterator f = m.find(10);
  if (f != m.end())
  {
  cout << "找到了" << endl;
  }
  else
  {
  cout << "没有找到" << endl;
  }
}
int main()
{
  test1();
  return 0;
}

2d65d23f6d4748949b924e4057485923.png


统计函数


可以统计这个容器有多少个这个键值,对于map来说,只有0和1,因为map是不可以拥有重复键值的

count(key);//统计键值key的个数


使用:


#include<iostream>
#include<map>
using namespace std;
void test1()
{
  map<int,int> m;
  for (int i = 0; i < 10; i++)
  {
  m.insert(pair<int,int>(i,i+10));
  }
  cout << m.count(1) << endl;
  cout << m.count(9) << endl;
}
int main()
{
  test1();
  return 0;
}


排序规则


与set一样,利用仿函数去改变排序规则

#include<iostream>
#include<map>
using namespace std;
class sort_big
{
public:
  bool operator()(int v1, int v2) const
  {
  return v1 > v2;
  }
};
void test1()
{
  map<int, int ,sort_big> m;
  for (int i = 0; i < 10; i++)
  {
  m.insert(make_pair(i,i+10));
  }
  ; for (map<int,int, sort_big>::iterator b = m.begin(); b != m.end(); b++)
  {
  cout << (*b).second << " ";
  }
}
int main()
{
  test1();
  return 0;
}

0a2653c851af460fa595bd959398a8f1.png


下一座石碑


这座石碑倒下了,露出了下一座石碑…


😘预知后事如何,关注新专栏,和我一起征服C++这座巨塔

🚀专栏:C++爬塔日记

🙉都看到这里了,留下你们的👍点赞+⭐收藏+📋评论吧🙉


目录
打赏
0
0
0
0
2
分享
相关文章
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
29 2
|
12天前
|
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
116 73
|
8月前
|
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
【C++高阶】探索STL的瑰宝 map与set:高效数据结构的奥秘与技巧
113 0
C++基础知识(八:STL标准库 Map和multimap )
C++ 标准模板库(STL)中的 map 容器是一种非常有用的关联容器,用于存储键值对(key-value pairs)。在 map 中,每个元素都由一个键和一个值组成,其中键是唯一的,而值则可以重复。
131 0
|
9月前
|
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
66 0
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
134 0
使用 entrySet 遍历 Map 类集合 KV
使用 entrySet 遍历 Map 类集合 KV
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
该文章详细介绍了ES6中Set和Map数据结构的特性和使用方法,并探讨了它们在前端开发中的具体应用,包括如何利用这些数据结构来解决常见的编程问题。
ES6的Set和Map你都知道吗?一文了解集合和字典在前端中的应用
java集合框架复习----(4)Map、List、set
这篇文章是Java集合框架的复习总结,重点介绍了Map集合的特点和HashMap的使用,以及Collections工具类的使用示例,同时回顾了List、Set和Map集合的概念和特点,以及Collection工具类的作用。
java集合框架复习----(4)Map、List、set