• STL:map/multimap容器详解


    map容器中所有元素都是pair,pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)。相信学过python的就知道这和python中的字典十分类似。同时,所有元素都会根据元素的键值自动排序。map/multimap属于关联式容器,底层数据结构是用二叉树实现的。它的优点就是可以根据key值快速找到value值。
    这里需要了解map与multimap的区别:
    即map不予许容器中有重复的key值元素;而multimap允许容器中有重复的key值元素,这里的区别与set与multiset十分类似

    1.map容器的构造与赋值

    函数描述
    map mmap默认构造函数
    map(const map &mp)拷贝构造函数
    map& operator=(const map &mp)重载等号操作符

    具体代码的使用如下所示:

    #include
    #include
    using namespace std;
    //map容器的构造与赋值
    void printMap(map<int,int> &m) {
    	for (auto it = m.begin(); it != m.end(); it++) {
    		cout << "key =" << it->first << " value =" << it->second << endl;
    	}
    	cout << endl;
    }
    int main() {
    	//创建map容器
    	map<int, int> m;
    	//插入数据里面需要传入的是对组
    	m.insert(pair<int, int>(1, 10));//pair(1, 10)为匿名二元组
    	m.insert(pair<int, int>(3, 30));
    	m.insert(pair<int, int>(4, 40));
    	m.insert(pair<int, int>(2, 20));
    	//插入元素后会根据key自动进行升序排列
    	printMap(m);
    	
    	//拷贝构造
    	map<int, int> m2(m);
    	printMap(m2);
    
    	//赋值
    	map<int, int> m3;
    	m3 = m2;
    	printMap(m3);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    这里尤其需要注意的是,map容器中所有的元素都是成对出现的,插入数据的时候要使用二元组,即对组pair。

    2.map容器的大小与交换

    函数描述
    size()返回容器中元素的数目
    empty()判断容器中是否为空
    swap(st)交换两个集合容器
    #include
    #include
    using namespace std;
    //map容器的大小与交换
    void printMap(map<int,int> &m) {
    	for (auto it = m.begin(); it != m.end(); it++) {
    		cout << "key值为:" << it->first << " value值为:" << it->second << endl;
    	}
    	cout << endl;
    }
    int main() {
    	map<int, int> m;
    	m.insert(pair<int, int>(1, 10));
    	m.insert(pair<int, int>(3, 30));
    	m.insert(pair<int, int>(2, 20));
    	if (m.empty()) {
    		cout << "m容器为空" << endl;
    	}
    	else {
    		cout << "m容器不为空" << endl;
    		cout << "m的大小为:" << m.size() << endl;
    	}
    	//交换
    	map<int, int> m2;
    	m2.insert(pair<int,int>(4, 40));
    	m2.insert(pair<int,int>(6, 60));
    	m2.insert(pair<int,int>(5, 50));
    	cout << "map容器交换前" << endl;
    	printMap(m);
    	printMap(m2);
    	cout << "map容器交换后" << endl;
    	m.swap(m2);
    	printMap(m);
    	printMap(m2);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    3.map容器的插入与删除

    函数描述
    insert(elem)在容器中插入元素
    clear()清除所有元素
    erase(pos)删除pos迭代器所指的元素,返回下一个元素的迭代器
    erase(beg,end)删除区间[beg,end)的所有元素,返回下一个元素的迭代器
    erase(key)删除容器中值为key的元素
    #include
    #include 
    using namespace std;
    //map容器的插入与删除操作
    void printMap(map<int,int> &m) {
    	for (auto it = m.begin(); it != m.end(); it++) {
    		cout << "key值为:" << it->first << " value值为:" << it->second << endl;
    	}
    	cout << endl;
    }
    int main() {
    	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));
    	//插入 第四种 最简单
    	//[]不建议插入 通过[]可以利用key访问到value
    	//使用[]插入元素的时候,如果key不存在将会自动创建键值对
    	m[4] = 40;
    	printMap(m);
    	//删除
    	m.erase(m.begin());
    	printMap(m);
    	//删除 直接传入key
    	m.erase(3);
    	printMap(m);
    	//全部删除
    	m.clear();//相当于m.erase(m.begin(),m.end())
    	printMap(m);
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    4.map容器的查找与统计

    函数描述
    find(key)查找key是否存在,返回该键的元素的迭代器;若不存在返回map.end()
    count(key)统计key的元素的个数
    #include
    #include
    using namespace std;
    int main() {
    	//查找
    	map<int, int> m;
    	m.insert(pair<int, int>(1, 10));
    	m.insert(pair<int, int>(3, 30));
    	m.insert(pair<int, int>(2, 20));
    	//查找键为3的键值对
    	map<int,int>::iterator pos=m.find(3);
    	if (pos != m.end()) {
    		cout << "查到了元素 key=" << pos->first << " value=" << pos->second << endl;
    	}
    	else {
    		cout << "未找到元素" << endl;
    	}
    	//统计
    	//由于map容器中key不能重复出现 因此count统计的结果只有0或1
    	int num=m.count(3);//返回结果为整型
    	cout << "num=" << num << endl;
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    这里需要注意的是,find返回的是迭代器,而count返回的是整型!

  • 相关阅读:
    HTML学习笔记
    Asp-Net-Core开发笔记:FrameworkDependent搭配docker部署
    vivado设置Vscode为默认编辑器
    【GoLang】常量变量
    Jenkins 构建Maven 项目
    java面试题总结
    AI网络爬虫023:用deepseek批量提取天工AI的智能体数据
    redis与 缓存击穿、缓存穿透、缓存雪崩
    什么是架构基本概念和架构本质
    「实用场景教程」如何用日程控件DHTMLX Scheduler制作酒店预订日历?(一)
  • 原文地址:https://blog.csdn.net/qq_51447436/article/details/126711431