容器map/multimap的使用方法

1.知识点

在这里插入图片描述

2.源程序

#include <iostream>
#include <string>
#include <tchar.h>
#include <map>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	//1.map对象的默认构造
	cout << "1.map对象的默认构造" << endl;

	map<int, char> map1;
	map<string, float>map2;

	cout << "\n\n";

	//2.map的插入与迭代器
	cout << "2.map的插入与迭代器" << endl;

	map<int, string> mapStu;

	//在map中插入元素的三种方式:
	//前两种方法,采用的是insert()方法,该方法返回值为pair<iterator,bool>

	//2.1 通过pair的方式插入对象
	cout << "2.1 通过pair的方式插入对象" << endl;
	mapStu.insert(pair<int, string>(3, "小张"));

	//2.2 通过value_type的方式插入对象
	cout << "2.2 通过value_type的方式插入对象" << endl;
	mapStu.insert(map<int,string>::value_type(1,"小李"));

	//2.3 通过数组的方式插入值
	//第三种方法非常直观,但存在一个性能的问题。
	//插入3时,先在mapStu中查找主键为3的项,若没发现,则将一个键为3,值为初始化值的对组插入到mapStu中,然后再将值修改成“小刘”。
	//若发现已存在3这个键,则修改这个键对应的value。
	cout << "2.3 通过数组的方式插入值" << endl;
	mapStu[4] = "小刘";
	mapStu[5] = "小王";

	//取操作或插入操作
	string strName = mapStu[2];//只有当mapStu存在2这个键时才是正确的取操作,否则会自动插入一个实例,键为2,值为初始化值。

	//遍历:法一 采用迭代器
	cout << "遍历:法一 采用迭代器" << endl;
	for (map<int, string>::iterator it = mapStu.begin(); it != mapStu.end(); ++it)
	{
		pair<int, string> pr = *it;//注意这里
		int iKey = pr.first;//或 int iKey = it->first;
		string strValue = pr.second;//或 string strValue = it->second;

		cout << "mapStu的关键字key:" << pr.first << "  " << "mapStu的关键字的值value:" << pr.second << endl;
	}
	cout << "\n\n";

	//遍历:法二 采用数组的方式
	cout << "遍历:法二 采用数组的方式" << endl;
	for (int it = 1; it < 6; ++it)
	{
		cout << "mapStu的关键字key:" << it << "  " << "mapStu的关键字的值value:" << mapStu[it] << endl;
	}
	cout << "\n\n";

	//3.map的查找
	cout << "3.map的查找" << endl;

	map<int, string> mapA;
	mapA.insert(pair<int, string>(3, "小张"));
	mapA.insert(pair<int, string>(1, "小杨"));
	mapA.insert(pair<int, string>(7, "小赵"));
	mapA.insert(pair<int, string>(5, "小王"));

	//3.1 map.find(key);查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();
	cout << "3.1 map.find(key)" << endl;

	map<int, string>::iterator itFind = mapA.find(3);
	if (itFind == mapA.end())
	{
		//没有找到
		cout << "没有找到" << endl;
	}
	else
	{
		//找到了
		pair<int, string>pairMapA = *itFind;
		int iID = itFind->first;
		string strName = itFind->second;
		cout << "mapA的关键字key:" << iID << "  " << "mapA的关键字的值value:" << strName << endl;
	}
	cout << "\n\n";

	//3.2 map.lower_bound(keyElem);返回第一个key>=keyElem元素的迭代器。
	cout << "3.2 map.lower_bound(keyElem);返回第一个key>=keyElem元素的迭代器。" << endl;

	map<int, string>::iterator itLower_bound = mapA.lower_bound(3);

	cout << "mapA的关键字key:" << itLower_bound->first << "  " << "mapA的关键字的值value:" << itLower_bound->second << endl << endl;

	//3.3 map.upper_bound(keyElem);返回第一个key>keyElem元素的迭代器。
	cout << "3.3 map.upper_bound(keyElem);返回第一个key>keyElem元素的迭代器。" << endl;

	map<int, string>::iterator itUpper_bound = mapA.upper_bound(3);
	pair<int, string>pairItUpper_bound = *itUpper_bound;//这里采取另一种方式获取map里面的数据,对比3.2

	cout << "mapA的关键字key:" << pairItUpper_bound.first << "  " << "mapA的关键字的值value:" << pairItUpper_bound.second << endl << endl;

	//3.4 map.equal_range(keyElem);		
	//返回容器中key与keyElem相等的上下限的两个迭代器。上限是闭区间,下限是开区间,如[beg,end)。
    //以上函数返回两个迭代器,而这两个迭代器被封装在pair中。

	cout << "3.4 map.equal_range(keyElem);	" << endl;

	pair<map<int, string>::iterator, map<int, string>::iterator> pairEqual_range = mapA.equal_range(3);

	cout << "mapA的第一个关键字key:" << (pairEqual_range.first)->first << "  " << "mapA的第一个关键字的值value:" << (pairEqual_range.first)->second << endl;
	cout << "mapA的第二个关键字key:" << (pairEqual_range.second)->first << "  " << "mapA的第二个关键字的值value:" << (pairEqual_range.second)->second << endl << endl;

	return 0;
}

3.运行结果

在这里插入图片描述

C++ STL中的mapmultimap是关联容器,用于存储键值对(key-value pairs),其中每个键(key)唯一对应一个值(value)。 map是一个有序容器,根据键的大小进行自动排序,默认按照键的升序进行排序。每个键只能在map中出现一次,如果尝试插入具有相同键的元素,新元素将替代旧元素。 multimap也是一个有序容器,与map不同的是,它允许多个具有相同键的元素存在。多个具有相同键的元素将按照插入的顺序进行存储,而不会自动排序。 这两个容器都提供了一系列的操作函数,如insert、erase、find等,用于插入、删除和查找元素。 以下是一个使用map的简单示例: ```cpp #include <iostream> #include <map> int main() { std::map<std::string, int> scores; scores.insert(std::make_pair("Alice", 90)); scores.insert(std::make_pair("Bob", 80)); scores.insert(std::make_pair("Charlie", 70)); // 查找并输出Bob的分数 std::cout << "Bob's score: " << scores["Bob"] << std::endl; // 遍历并输出所有键值对 for (const auto& pair : scores) { std::cout << pair.first << ": " << pair.second << std::endl; } return 0; } ``` 上述示例中,我们创建了一个存储string类型键和int类型值的map容器scores。通过insert函数依次插入了三个键值对。然后我们通过scores["Bob"]来获取Bob的分数,并输出结果为80。 接着我们使用范围-based for循环遍历map中的所有键值对,并输出每个键值对的键和值。 multimap的用法与map类似,只是它允许多个具有相同键的元素存在。 这些关联容器在查找和插入操作上具有较高的效率,特别适用于需要根据键进行快速查找的场景。在实际应用中,你可以根据自己的需求选择适合的容器类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值