【C++常用容器】STL基础语法学习&map容器

本文涉及的产品
容器镜像服务 ACR,镜像仓库100个 不限时长
简介: map中的所有元素都是pair,pair中第一个元素为key(键值),第二个元素为value(实值),并且所有元素会根据元素的键值自动进行从小到大的排序。它可以根据key的值快速的找到value的值。map与multimap为关联式容器,map不允许容器中有重复的key值,而multimap允许容器中有重复的key值,两者底层的原理是用二叉树实现的。

●map基本概念


       map中的所有元素都是pair,pair中第一个元素为key(键值),第二个元素为value(实值),并且所有元素会根据元素的键值自动进行从小到大的排序。它可以根据key的值快速的找到value的值。map与multimap为关联式容器,map不允许容器中有重复的key值,而multimap允许容器中有重复的key值,两者底层的原理是用二叉树实现的。


●map构造和赋值


函数原型:


       1.构造


       ■map<T1, T2> mp         //map默认构造函数


       ■map(const map &mp)         //拷贝构造函数


       2.赋值


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


#include<iostream>
#include<map>
using namespace std;
void printmap(map<int,int>&m)
{
  for (map<int, int>::iterator i = m.begin(); i != m.end(); i++)
  {
  cout << "key=" << i->first << " " << "value=" << i->second << endl;
  }
  cout << endl;
}
void text()
{
  map<int, int>m;
  for (int i = 1, j = 10; i <= 10; i++, j += 10)
  {
  m.insert(pair<int,int>(i,j));
  }
  //拷贝构造
  map<int, int>m1(m);
  printmap(m1);
  //赋值
  map<int, int>m2;
  m2 = m1;
  printmap(m2);
}
int main()
{
  text();
}

0b7c4cced1389c33d417ca1bea68b5f0_d86fd479118c411a89843abe776f86aa.png


●map大小和交换


函数原型:


       1.大小


       ■size()         //返回容器中元素的数目


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


       2.交换


       ■swap(st)         //交换两个集合容器


#include<iostream>
#include<map>
using namespace std;
void swap(map<int, int>& m1, map<int, int>& m2)
{
  m1.swap(m2);
}
void issize(map<int, int>& m)
{
  cout << "map的大小为:" << m.size() << endl;
}
void isempty(map<int,int>&m)
{
  if (m.empty()) {
  cout << "map为空" << endl;
  }
  else {
  cout << "map不为空" << endl;
  issize(m);
  }
}
void printmap(map<int,int>&m)
{
  for (map<int, int>::iterator i = m.begin(); i != m.end(); i++)
  {
  cout <<"value=" << i->second << endl;
  }
  cout << endl;
}
void text()
{
  map<int, int>m;
  m.insert(pair<int, int>(1, 1));
  m.insert(pair<int, int>(2, 3));
  m.insert(pair<int, int>(3, 5));
  m.insert(pair<int, int>(4, 7));
  m.insert(pair<int, int>(5, 9));
  //判断是否为空,大小为多少
  isempty(m);
  //交换
  map<int, int>m1;
  m1.insert(pair<int, int>(1, 2));
  m1.insert(pair<int, int>(2, 4));
  m1.insert(pair<int, int>(3, 6));
  m1.insert(pair<int, int>(4, 8));
  m1.insert(pair<int, int>(5, 10));
  cout << "交换前:" << endl;
  printmap(m);
  printmap(m1);
  cout << "交换后:" << endl;
  swap(m,m1);
  printmap(m);
  printmap(m1);
}
int main()
{
  text();
}

71719ef44035b54f76ed557ce802a4dd_6db9f249b5f84f2b9b0ea17394844a20.png


●map插入和删除


函数原型:


       1.插入


       ■insert(elem); //在容器中插入元素


       2.删除


       ■clear()         //清除所有元素


       ■erase(pos)         //删除pos迭代器所指的元素,返回下一个元素的迭代器


       ■erase(beg,end)         //删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器


       ■erase(key)         //删除容器中值为key的元素


#include<iostream>
#include<map>
using namespace std;
void printmap(map<int,int>&m)
{
  for (map<int, int>::iterator i = m.begin(); i != m.end(); i++)
  {
  cout << "key=" << i->first << " " << "value=" << i->second << endl;
  }
  cout << endl;
}
void text()
{
  map<int, int>m;
  //插入
  for (int i = 1, j = 1; i <= 5; i++, j += 1)
  {
  m.insert(pair<int,int>(i,j));
  }
  for (int i = 6, j = 7; i <= 10; i++, j += 1)
  {
  m.insert(make_pair(i, j));
  }
  for (int i = 11, j = 10; i <= 15; i++, j += 1)
  {
  m[i] = j;
  }
  printmap(m);
  //删除
  m.erase(m.begin());
  m.erase(2);
  m.erase(3);
  m.erase(13);
  m.erase(14);
  printmap(m);
  //清空
  //m.erase(m.begin(), m.end());
  m.clear();
  printmap(m);
}
int main()
{
  text();
}

9e9d55598ec0679f8abee5e268211068_1fe8a09cb71f4860a8e767455932d7ed.png


●map查找和统计


函数原型:


       1.查找


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


       2.统计


       ■count(key)         //统计key的元素个数    

#include<iostream>
#include<map>
using namespace std;
void count(map<int, int>& m)
{
  cout << "请统计指定key下的元素个数:"<<endl;
  int n; cin >> n;
  cout << "num=" << m.count(n) << endl;
  //map不允许插入重复key元素,所以统计结果要么1,要么0
} 
void find(map<int,int>&m)
{
  cout << "请输入要查找的key:" << endl;
  int n; cin >> n;
  map<int,int>::iterator i=m.find(n);
  if (i!=m.end()) {
  cout << "该map容器中的元素:" << i->second << endl;
  }
  else {
  cout << "未找到元素" << endl;
  }
}
void printmap(map<int,int>&m)
{
  for (map<int, int>::iterator i = m.begin(); i != m.end(); i++)
  {
  cout << "key=" << i->first << " " << "value=" << i->second << endl;
  }
  cout << endl;
}
void text()
{
  map<int, int>m;
  for (int i = 1, j = 10; i <= 10; i++, j += 10)
  {
  m.insert(make_pair(i,j));
  }
  m.insert(make_pair(10,100));
  printmap(m);
  //查找
  find(m);
  //统计
  count(m);
}
int main()
{
  text();
}

87df20cdc7e07b593a9d83a3c3cff178_19c12b915d2844f9af7208c1bc9bd795.png


●map排序(map初始排序顺序为从小到大,用仿函数将其改为从大到小)


       1.内置数据类型排序

#include<iostream>
#include<map>
using namespace std;
class compare {
public:
  bool operator()(int m1,int m2) const
  {
  return m1 > m2;
  }
};
void printmap(map<int, int, compare>&m)
{
  for (map<int, int, compare>::iterator i = m.begin(); i != m.end(); i++)
  {
  cout << "key=" << i->first << " " << "value=" << i->second << endl;
  }
}
void text()
{
  map<int, int,compare>m;
  for (int i = 1, j = 10; i <= 10; i++, j += 10)
  {
  m.insert(make_pair(i,j));
  }
  printmap(m);
}
int main()
{
  text();
}

4a0f40537c7f73389fd242e2ce76e186_c1657d41f14649dfafa808d78dd4d0e1.png


       2.自定义数据类型排序


#include<iostream>
#include<map>
#include<string>
using namespace std;
class person {
public:
  string name;
  int age;
  person(string name, int age)
  {
  this->name = name;
  this->age = age;
  }
};
class compare {
public:
  bool operator()(int m1,int m2)const
  {
  return m1 > m2;
  }
};
void printmap(map<int, person, compare>& m)
{
  for (map<int, person, compare>::iterator i = m.begin(); i != m.end(); i++)
  {
  cout << "key=" << i->first << " " <<"姓名:"<<i->second.name << "年龄:" << i->second.age << endl;
  }
}
void text()
{
  person p1("张三", 19);
  person p2("李四", 20);
  person p3("王五", 21);
  map<int, person, compare>m;
  m.insert(make_pair(1, p1));
  m.insert(make_pair(2, p2));
  m.insert(make_pair(3, p3));
  printmap(m);
}
int main()
{
  text();
}

55506ab8ffb0d05b80d5a20fac11f641_36c9aa4293a243239284c8708b006be2.png



目录
相关文章
|
6月前
|
编译器 C++ 容器
【c++丨STL】基于红黑树模拟实现set和map(附源码)
本文基于红黑树的实现,模拟了STL中的`set`和`map`容器。通过封装同一棵红黑树并进行适配修改,实现了两种容器的功能。主要步骤包括:1) 修改红黑树节点结构以支持不同数据类型;2) 使用仿函数适配键值比较逻辑;3) 实现双向迭代器支持遍历操作;4) 封装`insert`、`find`等接口,并为`map`实现`operator[]`。最终,通过测试代码验证了功能的正确性。此实现减少了代码冗余,展示了模板与仿函数的强大灵活性。
173 2
|
6月前
|
存储 算法 C++
【c++丨STL】map/multimap的使用
本文详细介绍了STL关联式容器中的`map`和`multimap`的使用方法。`map`基于红黑树实现,内部元素按键自动升序排列,存储键值对,支持通过键访问或修改值;而`multimap`允许存在重复键。文章从构造函数、迭代器、容量接口、元素访问接口、增删操作到其他操作接口全面解析了`map`的功能,并通过实例演示了如何用`map`统计字符串数组中各元素的出现次数。最后对比了`map`与`set`的区别,强调了`map`在处理键值关系时的优势。
333 73
|
7月前
|
存储 缓存 C++
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
C++ 标准模板库(STL)提供了一组功能强大的容器类,用于存储和操作数据集合。不同的容器具有独特的特性和应用场景,因此选择合适的容器对于程序的性能和代码的可读性至关重要。对于刚接触 C++ 的开发者来说,了解这些容器的基础知识以及它们的特点是迈向高效编程的重要一步。本文将详细介绍 C++ 常用的容器,包括序列容器(`std::vector`、`std::array`、`std::list`、`std::deque`)、关联容器(`std::set`、`std::map`)和无序容器(`std::unordered_set`、`std::unordered_map`),全面解析它们的特点、用法
C++ 容器全面剖析:掌握 STL 的奥秘,从入门到高效编程
|
7月前
|
编译器 C语言 C++
☺初识c++(语法篇)☺
☺初识c++(语法篇)☺
|
10月前
|
存储 C++ 容器
【C++】map、set基本用法
本文介绍了C++ STL中的`map`和`set`两种关联容器。`map`用于存储键值对,每个键唯一;而`set`存储唯一元素,不包含值。两者均基于红黑树实现,支持高效的查找、插入和删除操作。文中详细列举了它们的构造方法、迭代器、容量检查、元素修改等常用接口,并简要对比了`map`与`set`的主要差异。此外,还介绍了允许重复元素的`multiset`和`multimap`。
238 3
【C++】map、set基本用法
|
10月前
|
存储 算法 C++
【C++】unordered_map(set)
C++中的`unordered`容器(如`std::unordered_set`、`std::unordered_map`)基于哈希表实现,提供高效的查找、插入和删除操作。哈希表通过哈希函数将元素映射到特定的“桶”中,每个桶可存储一个或多个元素,以处理哈希冲突。主要组成部分包括哈希表、哈希函数、冲突处理机制、负载因子和再散列,以及迭代器。哈希函数用于计算元素的哈希值,冲突通过开链法解决,负载因子控制哈希表的扩展。迭代器支持遍历容器中的元素。`unordered_map`和`unordered_set`的插入、查找和删除操作在理想情况下时间复杂度为O(1),但在冲突较多时可能退化为O(n)。
200 5
|
10月前
|
存储 C++ 容器
【C++】map的模拟实现
C++中的`map`是STL中的一种关联容器,存储键值对且键唯一。`map`基于红黑树实现,自动按键排序,支持动态调整、复杂数据类型、丰富的成员函数及双向迭代器。插入、查找等操作保证了对数时间复杂度,适用于需要快速查找和有序存储的场景。
142 3
|
10月前
|
存储 设计模式 C++
【C++】优先级队列(容器适配器)
本文介绍了C++ STL中的线性容器及其适配器,包括栈、队列和优先队列的设计与实现。详细解析了`deque`的特点和存储结构,以及如何利用`deque`实现栈、队列和优先队列。通过自定义命名空间和类模板,展示了如何模拟实现这些容器适配器,重点讲解了优先队列的内部机制,如堆的构建与维护方法。
149 0
|
11月前
|
存储 缓存 Java
【用Java学习数据结构系列】HashMap与TreeMap的区别,以及Map与Set的关系
【用Java学习数据结构系列】HashMap与TreeMap的区别,以及Map与Set的关系
137 1
|
11月前
|
存储 编译器 C++
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
【C++篇】揭开 C++ STL list 容器的神秘面纱:从底层设计到高效应用的全景解析(附源码)
188 2