容器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.运行结果

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值