C++系列-STL容器中的map容器

2024-09-04 17:04
文章标签 c++ 系列 容器 map stl

本文主要是介绍C++系列-STL容器中的map容器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

STL容器中的map容器

  • map的基本概念
  • map的特点
  • map的构造
  • map的赋值
  • map的插入
  • map的删除
  • map的大小
  • map的交换查找,统计
  • map的排序


无题二首·其一 李商隐
昨夜星辰昨夜风,画楼西畔桂堂东。
身无彩凤双飞翼,心有灵犀一点通。
隔座送钩春酒暖,分曹射覆蜡灯红。
嗟余听鼓应官去,走马兰台类转蓬。


map的基本概念

  • map是一种关联容器,也被称为字典,底层结构是二叉树。
  • 它以键值对key,value的形式存储元素。其中key是唯一的,用于快速查找对应的value。
  • 如果尝试插入具有相同key的元素,旧的值将被新的覆盖。
  • map提供双向迭代器,可以遍历容器中的所有键值对。
  • 迭代器指向的是pair类型的对象,first成员属性是key,second成员属性是value
  • multimap的key值是可以重复的,其它的与map类似。

map的特点

  • 有序性,map中的元素是按照key的特定顺序存储的,默认情况下是key的升序排列,可以按照顺序遍历map中的元素,并可以进行范围查询等操作。
  • map中的key有唯一性,multimap则无。
  • 高效的查找和插入。
  • map以键值对的方式存储元素,方便将相关的数据关联在一起。
  • 可以通过自定义的比较函数来改变key的排序规则。

map的构造

构造函数描述
map<type1, type2> mp1默认构造函数
map<type1, type2> mp2(mp1)拷贝构造
map<type1, type2> mp2{{},{}…}初始化列表构造
map<type1, type2> mp4(迭代器1, 迭代器2范围构造
map<type1, type2> mp5(move(mp1)直接移动,并不拷贝
code:
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;template<typename T1, typename T2>
void print_map(const map<T1, T2>& mp)
{for (auto i_mp: mp){cout << "first: " << i_mp.first << ", second: " << i_mp.second << endl;}
}
void test01()
{// 默认构造map<string, int> mp1;pair<string, int> pr1 ("Lily", 9);pair<string, int> pr2 ("Lucy", 10);mp1.insert(pr1);mp1.insert(pr2);cout << "---------- mp1, mp1.size() ---------- " << mp1.size() << endl;print_map(mp1);cout << "value of key Lily: " << mp1["Lily"] << endl;	// 访问元素value的方式// 拷贝构造map<string, int> mp2(mp1);cout << "---------- map<string, int> mp2(mp1), mp2 ---------- " << mp2.size() << endl;print_map(mp2);// 初始化列表构造map<string, int> mp3{ {"Hanmeimei", 9}, { "Poly", 2 } }) cout << "---------- map<string, int> mp3({{},{},...}), mp3 ---------- " << mp3.size() << endl;print_map(mp3);// 范围构造vector<pair<string, int>> vec1 = { {"张三", 5}, {"李四", 6}, {"王五", 7} };map<string, int> mp4(vec1.begin(), vec1.begin() + 2);cout << "---------- map<string, int> mp4(vec1.begin(), vec1.begin() + 2), mp4 ---------- " << mp4.size() << endl;print_map(mp4);// 移动构造,move括号中的元素空间会直接移动cout << "---------- 移动构造前 ----------" << endl;cout << "mp3.size(): " << mp3.size() << endl;map<string, int> mp5(move(mp3));cout << "---------- 移动构造后 ----------" << endl;cout << "---------- map<string, int> mp5(move(mp3)) ---------- " << mp5.size() << endl;print_map(mp5);cout << "mp3.size(): " << mp3.size() << endl;print_map(mp3);
}
int main()
{test01();system("pause");return 0;
}result:
---------- mp1, mp1.size() ---------- 2
first: Lily, second: 9
first: Lucy, second: 10
value of key Lily: 9
---------- map<string, int> mp2(mp1), mp2 ---------- 2
first: Lily, second: 9
first: Lucy, second: 10
---------- map<string, int> mp3({{},{},...}), mp3 ---------- 2
first: Hanmeimei, second: 9
first: Poly, second: 2
---------- map<string, int> mp4(vec1.begin(), vec1.begin() + 2), mp4 ---------- 2
first: 李四, second: 6
first: 张三, second: 5
---------- 移动构造前 ----------
mp3.size(): 2
---------- 移动构造后 ----------
---------- map<string, int> mp5(move(mp3)) ---------- 2
first: Hanmeimei, second: 9
first: Poly, second: 2
mp3.size(): 0

map的赋值

  • 使用=重载
code:
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
using namespace std;template<typename T1, typename T2>
void print_map(const map<T1, T2>& mp)
{for (auto i_mp : mp){cout << "first: " << i_mp.first << ", second: " << i_mp.second << endl;}
}
void test01()
{// = 赋值map<string, int> mp1{{"Lily", 9}, { "Lucy", 10 }};cout << "---------- mp1, mp1.size() ---------- " << mp1.size() << endl;print_map(mp1);map<string, int> mp2 = mp1;cout << "---------- map<string, int> mp2 = mp1, mp2 ---------- " << mp2.size() << endl;print_map(mp2);
}
int main()
{test01();system("pause");return 0;
}result:
---------- mp1, mp1.size() ---------- 2
first: Lily, second: 9
first: Lucy, second: 10
---------- map<string, int> mp2 = mp1, mp2 ---------- 2
first: Lily, second: 9
first: Lucy, second: 10

map的插入

构造函数描述
pair<iterator, bool> insert(pair<type ,type2>(key, value))insert插入pair,返回值是pair,第一个元素时迭代器,迭代器所指值的第一个元素是key,第二个是value,pair中的second元素bool类型表示是否插入成功
pair<iterator, bool> insert(make_pair(key, value))insert插入pair,pair由make_pair产生,返回值同上
pair<iterator, bool> insert(map<type1, type2>::value_type(key, value))使用type_value函数产生pair对象,返回值同上
mapped_type& mp1[value]=value[]插入法, 如果插错了,比如key写错了,会自动创建一个对应这个key的元素,value设置为0,建议少用,但[]通过key访问value很方便
void insert({ val1, val2, … })一次向 map 容器中插入多个键值对
void insert (InputIterator first, InputIterator last)插入某map容器中的指定区域
iterator insert (const_iterator position, const value_type& val)会按照key自动排序,不按照position的指示
code:
#include <iostream>
#include <map>
using namespace std;template<typename T1, typename T2>
void print_map(const map<T1, T2>& mp)
{for (auto i_mp : mp){cout << "first: " << i_mp.first << ", second: " << i_mp.second << endl;}
}
void test01()
{// pair<iterator, bool> insert(pair<type, type2>(key, value)map<string, int> mp1{{"Lily", 9}, { "Lucy", 10 }};//pair<map<string, int>::iterator, bool> pr1 = mp1.insert(pair<string, int>("Jim", 10));auto pr1 = mp1.insert(pair<string, int>("Jim", 10));	// 可以用auto,更简单cout << "----- pr1.first->first: " << pr1.first->first << ", pr1.first->second: " << pr1.first->second << ", pr1.second: " << pr1.second << endl;// pair<iterator, bool> insert(make_pair(key, value)auto pr2 = mp1.insert(make_pair("Poly", 2));cout << "----- pr2.first->first: " << pr2.first->first << ", pr2.first->second: " << pr2.first->second << ", pr2.second: " << pr2.second << endl;// iterator insert(map<type1, type2>::value_type(key, value))auto pr3 = mp1.insert(map<string, int>::value_type("Hanmeimei", 11));		// value_type返回是pair对象cout << "----- pr3.first->first: " << pr3.first->first << ", pr3.first->second: " << pr3.first->second << ", pr3.second: " << pr3.second << endl;// mapped_type& mp1[key]= valuemp1["MissGao"] = 33;// void insert({ val1, val2, ... });mp1.insert({ { "张三", 12 }, { "李四", 11 }, { "王五", 11 } });print_map(mp1);
}
int main()
{test01();system("pause");return 0;
}result:
----- pr1.first->first: Jim, pr1.first->second: 10, pr1.second: 1
----- pr2.first->first: Poly, pr2.first->second: 2, pr2.second: 1
----- pr3.first->first: Hanmeimei, pr3.first->second: 11, pr3.second: 1
first: Hanmeimei, second: 11
first: Jim, second: 10
first: Lily, second: 9
first: Lucy, second: 10
first: MissGao, second: 33
first: Poly, second: 2
first: 李四, second: 11
first: 王五, second: 11
first: 张三, second: 12

code:
#include <iostream>
#include <map>
using namespace std;template<typename T1, typename T2>
void print_map(const map<T1, T2>& mp)
{for (auto i_mp : mp){cout << "first: " << i_mp.first << ", second: " << i_mp.second << endl;}
}
void test01()
{cout << "---------- mp1 ----------" << endl;map<string, int> mp1{{"Lily", 9}, { "Lucy", 10 }};mp1.insert({ { "张三", 12 }, { "李四", 11 }, { "王五", 11 } });print_map(mp1);cout << "---------- mp2 ----------" << endl;map<string, int> mp2;auto it = mp1.find("王五");// void insert (InputIterator first, InputIterator last) //插入某map容器中的指定区域mp2.insert(mp1.begin(), it);print_map(mp2);cout << "---------- mp3 ----------" << endl;map<string, int> mp3{{"吴彦祖", 48}, { "金城武", 46 }};// iterator insert (const_iterator position, const value_type& val), 会按照key自动排序,不按照position的指示map<string, int>::iterator iter1 = mp3.insert(mp3.find("吴彦祖"), {"古天乐", 18});print_map(mp3);cout << "\nreturn value check" << endl;cout << "first: " << iter1->first << ", second: " << iter1->second << endl;
}
int main()
{test01();system("pause");return 0;
}result:
---------- mp1 ----------
first: Lily, second: 9
first: Lucy, second: 10
first: 李四, second: 11
first: 王五, second: 11
first: 张三, second: 12
---------- mp2 ----------
first: Lily, second: 9
first: Lucy, second: 10
first: 李四, second: 11
---------- mp3 ----------
first: 古天乐, second: 18
first: 金城武, second: 46
first: 吴彦祖, second: 48return value check
first: 古天乐, second: 18

map的删除

构造函数描述
iterator erase(const_iterator position)有的版本是返回删除的元素的迭代器,有的是返回其下一个
size_type erase (const key_type& k)返回删除了几个元素,map只能为0和1,multimap可以有多个
iterator erase(const_iterator first, const_iterator last)有的版本是返回删除的元素的迭代器,有的是返回其下一个
code:
#include <iostream>
#include <map>
using namespace std;template<typename T1, typename T2>
void print_map(const map<T1, T2>& mp)
{for (auto i_mp : mp){cout << "first: " << i_mp.first << ", second: " << i_mp.second << endl;}
}
void test01()
{cout << "---------- mp1 ----------" << endl;map<string, int> mp1{{"Lily", 9}, { "Lucy", 10 }};mp1.insert({ { "张三", 12 }, { "李四", 11 }, { "王五", 11 } });print_map(mp1);//iterator erase(const_iterator position); 有的版本是返回删除的元素的迭代器,有的是返回其下一个map<string, int>::iterator it = mp1.erase(mp1.find("张三"));cout << "it->first: " << (--it)->first << endl;// size_type erase (const key_type& k)		返回删除了几个元素,map只能为0和1,multimap可以有多个int st1 = mp1.erase("李四");cout << "int st1 = mp1.erase(key)" << endl;cout << "---------- mp1 ----------" << endl;print_map(mp1);// iterator erase(const_iterator first, const_iterator last)it = mp1.erase(mp1.begin(), --mp1.end());cout << endl;cout << "mp1.erase(mp1.begin(), --mp1.end())" << endl;		//返回指向下一个元素的迭代器print_map(mp1);cout << "it->first: " << it->first << endl;mp1.clear();cout << "mp1.clear(), mp1.size(): " << mp1.size() << endl;print_map(mp1);
}
int main()
{test01();system("pause");return 0;
}result:
---------- mp1 ----------
first: Lily, second: 9
first: Lucy, second: 10
first: 李四, second: 11
first: 王五, second: 11
first: 张三, second: 12
it->first: 王五
int st1 = mp1.erase(key)
---------- mp1 ----------
first: Lily, second: 9
first: Lucy, second: 10
first: 王五, second: 11mp1.erase(mp1.begin(), --mp1.end())
first: 王五, second: 11
it->first: 王五
mp1.clear(), mp1.size(): 0

map的大小

构造函数描述
size()返回map容器的大小
empty()返回map容器是否为空

map的交换查找,统计

|:--------😐:-------------|
|find() |如果查找到,返回查找到的迭代器,否则返回end() |
|count()|map的统计count(),map返回0或者1 |
|sort()|map的排序 |

code:
#include <iostream>
#include <map>
using namespace std;template<typename T1, typename T2>
void print_map(const map<T1, T2>& mp)
{for (auto i_mp : mp){cout << "first: " << i_mp.first << ", second: " << i_mp.second << endl;}
}
void test01()
{map<string, int> mp1{{"Lily", 9}, { "Lucy", 10 }};mp1.insert({ { "张三", 12 }, { "李四", 11 }, { "王五", 11 } });cout << "---------- mp1.size() ----------, " << mp1.size() << endl;print_map(mp1);// map的交换map<string, int> mp2;mp2["1"] = 111;mp2["2"] = 222;mp2["3"] = 333;cout << "---------- swap前----------, " << endl;print_map(mp1);print_map(mp2);cout << "---------- swap后----------, " << endl;mp1.swap(mp2);print_map(mp1);print_map(mp2);// map的查找iterator find(const key_type& _Keyval),如果查找到,返回查找到的迭代器,否则返回end()map<string, int>::iterator it = mp2.find("2");cout << "---------- mp2.find(\"2\")----------, " << endl;cout << "it->first: " << (--it)->first << endl;// size_type count(const key_type& _Keyval) map的统计count(),map返回0或者1int mp_count = mp1.count("3");cout << "---------- mp1.count(\"1\")----------, " << endl;cout << "mp_count: " << mp_count << endl;
}
int main()
{test01();system("pause");return 0;
}result:
---------- mp1.size() ----------, 5
first: Lily, second: 9
first: Lucy, second: 10
first: 李四, second: 11
first: 王五, second: 11
first: 张三, second: 12
---------- swap前----------,
first: Lily, second: 9
first: Lucy, second: 10
first: 李四, second: 11
first: 王五, second: 11
first: 张三, second: 12
first: 1, second: 111
first: 2, second: 222
first: 3, second: 333
---------- swap后----------,
first: 1, second: 111
first: 2, second: 222
first: 3, second: 333
first: Lily, second: 9
first: Lucy, second: 10
first: 李四, second: 11
first: 王五, second: 11
first: 张三, second: 12
---------- mp2.find("2")----------,
it->first: 张三
---------- mp1.count("1")----------,
mp_count: 1

map的排序

  • map的排序都是按照key来排序的,在创建map容器时就要指定好排序方式。
  • 对于普通数据类型会自动按照升序,如果要改变,可以自己写排序方式。
  • 自定义类型在创建时需要指定好排序规则,不然系统不知道怎么给排序,无法创建
code:
#include <iostream>
#include <map>
using namespace std;class Person
{friend class MyComparePerson;template<typename T1, typename T2, typename T3>friend void print_map(const map<T1, T2, T3>& mp);template<typename Person, typename T2>friend void print_map(const map<Person, T2, MyComparePerson>& mp);public:Person(string name, int age){m_name = name;m_age = age;}
private:string m_name;int m_age;
};
template<class T>
class MyCompare
{
public:bool operator()(const T &v1, const T &v2) const		// 这是需要用const限定,不然报错{return v1 > v2;}
};class MyComparePerson
{
public:bool operator()(const Person &p1, const Person &p2) const		// 这是需要用const限定,不然报错{return p1.m_age > p2.m_age;}
};template<typename T1, typename T2>
void print_map(const map<T1, T2, MyCompare<string>>& mp)
{for (auto i_mp : mp){cout << "first: " << i_mp.first << ", second: " << i_mp.second << endl;}
}template<typename Person, typename T2>
void print_map(const map<Person, T2, MyComparePerson>& mp)
{for (auto i_mp : mp){cout << "key.m_name: " << i_mp.first.m_name << ", key.m_age: " << i_mp.first.m_age << ", value: " << i_mp.second << endl;}
}void test01()
{// 普通数据类型会自动按照升序,如果要改变,可以自己写排序方式// 在创建时就要指定排序方式map<string, int, MyCompare<string>> mp2{ {"1", 20}, { "2", 30 }, { "3", 60 } };print_map(mp2);
}void test02()
{// 自定义类型在创建时需要指定好排序规则,不然系统不知道怎么给排序,无法创建// 在创建时就要指定排序方式//map<Person, int, MyComparePerson> mp1;map<Person, int, MyComparePerson> mp1;Person p1("lily", 12);Person p2("Lucy", 15);Person p3("Jim", 17);mp1.insert({{p1, 33}, {p2, 22}, {p3, 77} });print_map(mp1);
}int main()
{test01();test02();system("pause");return 0;
}result:
first: 3, second: 60
first: 2, second: 30
first: 1, second: 20
key.m_name: Jim, key.m_age: 17, value: 77
key.m_name: Lucy, key.m_age: 15, value: 22
key.m_name: lily, key.m_age: 12, value: 33

这篇关于C++系列-STL容器中的map容器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136508

相关文章

如何将Tomcat容器替换为Jetty容器

《如何将Tomcat容器替换为Jetty容器》:本文主要介绍如何将Tomcat容器替换为Jetty容器问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Tomcat容器替换为Jetty容器修改Maven依赖配置文件调整(可选)重新构建和运行总结Tomcat容器替

C++ 中的 if-constexpr语法和作用

《C++中的if-constexpr语法和作用》if-constexpr语法是C++17引入的新语法特性,也被称为常量if表达式或静态if(staticif),:本文主要介绍C++中的if-c... 目录1 if-constexpr 语法1.1 基本语法1.2 扩展说明1.2.1 条件表达式1.2.2 fa

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C++中::SHCreateDirectoryEx函数使用方法

《C++中::SHCreateDirectoryEx函数使用方法》::SHCreateDirectoryEx用于创建多级目录,类似于mkdir-p命令,本文主要介绍了C++中::SHCreateDir... 目录1. 函数原型与依赖项2. 基本使用示例示例 1:创建单层目录示例 2:创建多级目录3. 关键注

C++从序列容器中删除元素的四种方法

《C++从序列容器中删除元素的四种方法》删除元素的方法在序列容器和关联容器之间是非常不同的,在序列容器中,vector和string是最常用的,但这里也会介绍deque和list以供全面了解,尽管在一... 目录一、简介二、移除给定位置的元素三、移除与某个值相等的元素3.1、序列容器vector、deque

C++常见容器获取头元素的方法大全

《C++常见容器获取头元素的方法大全》在C++编程中,容器是存储和管理数据集合的重要工具,不同的容器提供了不同的接口来访问和操作其中的元素,获取容器的头元素(即第一个元素)是常见的操作之一,本文将详细... 目录一、std::vector二、std::list三、std::deque四、std::forwa

C++字符串提取和分割的多种方法

《C++字符串提取和分割的多种方法》在C++编程中,字符串处理是一个常见的任务,尤其是在需要从字符串中提取特定数据时,本文将详细探讨如何使用C++标准库中的工具来提取和分割字符串,并分析不同方法的适用... 目录1. 字符串提取的基本方法1.1 使用 std::istringstream 和 >> 操作符示

C++原地删除有序数组重复项的N种方法

《C++原地删除有序数组重复项的N种方法》给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度,不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(... 目录一、问题二、问题分析三、算法实现四、问题变体:最多保留两次五、分析和代码实现5.1、问题分析5.

C++ 各种map特点对比分析

《C++各种map特点对比分析》文章比较了C++中不同类型的map(如std::map,std::unordered_map,std::multimap,std::unordered_multima... 目录特点比较C++ 示例代码 ​​​​​​代码解释特点比较1. std::map底层实现:基于红黑

C++中函数模板与类模板的简单使用及区别介绍

《C++中函数模板与类模板的简单使用及区别介绍》这篇文章介绍了C++中的模板机制,包括函数模板和类模板的概念、语法和实际应用,函数模板通过类型参数实现泛型操作,而类模板允许创建可处理多种数据类型的类,... 目录一、函数模板定义语法真实示例二、类模板三、关键区别四、注意事项 ‌在C++中,模板是实现泛型编程