map映射

map映射

   map是STL的一种有序无重复的关联容器。关联容器与序列容器不同,他们的元素是按照关键字来保存和访问的。
   map提供一对-(保存的是一种key-value的pair对象,key是关键字且唯一, value是关键字对应的值)的数据处理能力,由于这个特性,它完全有在我们处理一对一数据的时候,在编程上提供快速通道。
头文件#include<map>
名字空间using namespace std;

#include <iostream>
#include <map>
#include<string>

using namespace std;

int main()
{
//1、如何构造map
	map<int, string>map_test1;//构造一个空的map
	map<int, string>map_test2(map_test1.begin(), map_test1.end());//构造一个 map_test1
	map<int, string>map_test3(map_test1);//拷贝构造
	map<string, string>map_test4{ { "test1", "123" }, { "test2", "456" }, { "test3", "789" } };//键值对构造

	//2、map插入三种方法:
	/***************************************************************************/

#if 1
	
	map_test1[1] = "test1";
	map_test1[2] = "test2";
	map_test1[3] = "test3";
	//这个操作会对第一个插入的数据进行覆盖
	map_test1[1] = "test4";
	//正序遍历
	for (map<int, string>::iterator iter = map_test1.begin(); iter != map_test1.end(); ++iter)
	{
		cout << "key: " <<iter->first<<",value: "<<iter->second<< endl;
	}
#endif//插入方法一;下标插入

#if 0
	map_test1.insert(pair<int, string>(4, "test4"));
	map_test1.insert(pair<int, string>(5, "test5"));
	//这个方法插入的话不会形成覆盖
	map_test1.insert(pair<int, string>(4, "test6"));
	//正序遍历
	for (map<int, string>::iterator iter = map_test1.begin(); iter != map_test1.end(); ++iter)
	{
		cout << "key: " << iter->first << ",value: " << iter->second << endl;
	}
#endif//插入方法二;pair插入方法

#if 0
	map_test1.insert(map<int, string>::value_type(6, "test6"));
	map_test1.insert(map<int, string>::value_type(7, "test7"));
	map_test1.insert(map<int, string>::value_type(8, "test8"));
	//这个方法插入的话不会形成覆盖
	map_test1.insert(map<int, string>::value_type(6, "test9"));
	//正序遍历
	for (map<int, string>::iterator iter = map_test1.begin(); iter != map_test1.end(); ++iter)
	{
		cout << "key: " << iter->first << ",value: " << iter->second << endl;
	}
#endif//插入方法三;map<int, string>::value_type插入方法

   /***************************************************************************/
	
	//3、map遍历三种方法:
   /***************************************************************************/

#if 0
	map<string, string>::iterator iter;
	for (iter = map_test4.begin(); iter != map_test4.end(); ++iter)
	{
		cout << "key: " << iter->first << ",value: " << iter->second << endl;
	}
#endif//遍历方法一:正向遍历器(效果是正序的,会自动排序)
	
#if 0
	map<string, string>::reverse_iterator iter2;
	for (iter2 = map_test4.rbegin(); iter2 != map_test4.rend(); ++iter2)
	{
		cout << "key: " << iter2->first << ",value: " << iter2->second << endl;
	}
#endif//遍历方法二;反向遍历器(效果是倒序的,会自动排序)

#if 0
	//下标访问key值遍历
	cout << map_test4["test1"] << endl;
	cout << map_test4["test3"] << endl;

	//可以使用.at()访问
	cout << map_test4.at("test2") << endl;

#endif//遍历方法三;key值访问(效果是只会输出value值,但是能访问指定的值,不会进行排序)

   /***************************************************************************/

	//4、查找并获取map中元素,三种方法
	/***************************************************************************/

#if 0
	//1.用count函数判断关键字是否有元素,有是1 没有是0
	//是一个bool类型.
	//PS:count()是用key值访问
	cout << map_test1.count(1) << endl;
	cout << map_test1.count(0) << endl;
#endif//查找方法一:用count函数判断关键字是否有元素

#if 0
	//2.用find 返回选代器 没找到返回end()
	//是一个迭代器类型
	//PS:find()是用key值访问
	map<int, string>::iterator it= map_test1.find(1);
	if (it != map_test1.end())
	{
		//first访问key值
		cout <<"key:"<< it->first << endl;
		//second访问value值
		cout <<"value:"<< it->second << endl;
	}
	else
	{
		cout << "do not find" << endl;
	}
#endif//查找方法二:find遍历
#if 0
	//创建一个遍历器
	map<int, string>::iterator it1;

	//寻找到>=的第一个元素位置
	it1 = map_test1.lower_bound(3);
	//寻找到>的第一个元素位置
	it1 = map_test1.upper_bound(1);

#endif//查找方法三:1、lower_bound() and 2、upper_bound()

	/***************************************************************************/

	//5、删除元素:erase(),三种方法
	/***************************************************************************/
	//创建一个迭代器
	map<int, string>::iterator it2;
	//删除第一个元素
	map_test1.erase(map_test1.begin());
	//key值删除
	map_test1.erase(2);
	//范围删除
	map_test1.erase(map_test1.begin(), map_test1.end());

	/***************************************************************************/
	system("pause");
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柳一航

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值