C++基础:C++标准库之map简介

1、综述

        Map是C++STL中众多的Container(容器)之一,与python的字典略类似,Map作为一个关联容器,将key与value相互关联,其中key为关键字,是不可更改的,而value是key值的相对应值。Map所提供的一对一的数据映射关系,在很多时候可以提供编程的极大便利。

        Map内部通过自建红黑树(一种非严格意义上的平衡二叉树)实现,可以对数据自动排序,因而在map内部的所有数据是有序存放的。Map具有的一大特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。

Map具有如下的功能特点:

  • 自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
  • 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
  • 快速插入Key - Value 记录。
  • 快速删除记录
  • 根据Key 修改value记录。
  • 遍历所有记录。

2、map的常见操作

首先,在使用map之前需包含头文件#include<map>,下面简要介绍map的常见操作,详细信息可参见http://www.cplusplus.com/reference/map/map/

(1)构造函数:

map<int,string> Mymap; 
map<int,string> Mymap2 (Mymap.begin(),Mymap.end());
map<int,string> Mymap3 (Mymap2); 
示例中int是key的类型,string是value的类型,可以为其他类型,如map<char,float> Mymap4; 


(2)插入数据:有几种方式,下面举三类例子。

Mymap[1] = "one"; //方式1
Mymap.insert(map<int,string>::value_type(11,"oneone"));//方式2
Mymap.insert(pair<int ,string>(111,"oneoneone"));//方式3
其中方式1简单直观,然而却并不值得提倡,因其性能并不好。在插入key = 1的value=“one”时,需要首先在Mymap,查找主键1是否已经存在,若不存在,则将一个新的对象插入Mymap,其key为1,但value是一个空字符串,插入完成后,将value赋为"one"; 该方法会首先将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。而另外的方法则有效避免了这一问题。

(3)查找元素:

利用find()方法,find()函数返回一个迭代器指向键值为key的元素,如果没找到就返回指向map尾部的迭代器。

map<int ,string>::iterator itr;
itr = Mymap.find(1);
if(itr!=Mymap.end())//如找不到,返回Mymap.end()
{
	cout<<"找到key为1的项:"<<endl;
	cout<< itr->first <<" "<<itr->second<<endl;//可以打印该项的key和value
}
else
	cout<<"找不到key为1的项!"<<endl;

(4)删除元素:

采用erase()方法实现:

	itr = Mymap.find(1);
	if(itr!=Mymap.end())
	{
		Mymap.erase(itr);//通过迭代器对象删除
		//Mymap.erase(1);//也可以通过主键删除
	}

erase()也可以删除一个范围如:
Mymap.erase(Mymap.begin(),Mymap.end());//相当于Mymap.clear(),可以清除整个map的内容

(5)swap()方法:

要注意的是,在map中swap()方法进行的是对于两个map的交换,而非对map内两个元素的交换。形如:

Mymap.swap(Mymap2);//交换两个map

(6)size()方法:

返回map的大小,即元素的个数。

(7)empty()方法

判断map是否为空,若map为空,则返回true。

(8)begin()方法:

返回指向map头部的迭代器

(9)end()方法:

返回指向map尾部的迭代器

(10)count() 方法:

 返回指定元素出现的次数


3、常见操作程序实例

贴上一个简单的程序集运行结果,以便于更好的理解。

//测试map用法
#include<map>
#include<string>
#include<iostream>
using namespace std;

void test_map()
{
	map<int ,string > Mymap;//key为int型,value为string型
	map<int ,string > Mymap2;
	pair<int ,string> Mypair;
	Mypair.first = 1;
	Mypair.second = "one";
	Mymap.insert(Mypair);//添加数据方式1
	Mymap.insert(map<int,string>::value_type(11,"oneone"));//添加数据方式二
	Mymap2[2] = "two"; //添加数据方法三,效率较差,不提倡
	Mymap2[22] = "twotwo";
	Mymap2.insert(pair<int ,string>(222,"twotwotwo"));
	map<int ,string>::iterator itr;//迭代器

	cout<<"Mymap中的内容为:"<<endl;
	for(itr = Mymap.begin();itr!=Mymap.end();itr++)//元素遍历
	{
		cout<< itr->first <<" " << itr->second<<endl;
	}
	cout<<"Mymap2中的内容为:"<<endl;
	for(itr = Mymap2.begin();itr!=Mymap2.end();itr++)
	{
		cout<< itr->first <<" " << itr->second<<endl;
	}

	/////元素查找
	itr = Mymap.find(11);//查找map中某key对应的元素是否存在
	if(itr!=Mymap.end())//如找不到,返回Mymap.end()
	{
		cout<<"
找到key为11的项:"<<endl;
		cout<< itr->first <<" "<<itr->second<<endl;
		itr->second = "oneoneone";
		cout<<"该项value修改为:"<<Mymap[11]<<endl;
	}
	
	Mymap.swap(Mymap2);//注意:交换的是两个map
	cout<<"
执行交换后:"<<endl;
	cout<<"Mymap中的内容为:"<<endl;
	for(itr = Mymap.begin();itr!=Mymap.end();itr++)
	{
		cout<< itr->first <<" " << itr->second<<endl;
	}
	cout<<"Mymap2中的内容为:"<<endl;
	for(itr = Mymap2.begin();itr!=Mymap2.end();itr++)
	{
		cout<< itr->first <<" " << itr->second<<endl;
	}

	cout<<"
删除Mymap2中key为1的项"<<endl;
	itr = Mymap2.find(1);
	if(itr!=Mymap2.end())
	{
		Mymap2.erase(itr);//
		//Mymap2.erase(1);//按key删除,以上面的操作效果等效
	}
	cout<<"删除后Mymap2中的内容为:"<<endl;
	for(itr = Mymap2.begin();itr!=Mymap2.end();itr++)
	{
		cout<< itr->first <<" " << itr->second<<endl;
	}
	if(!Mymap.empty())
	{
		cout<<"
Mymap大小为:"<<Mymap.size()<<endl;
		Mymap.clear();
		//Mymap.erase(Mymap.begin(),Mymap.end());//与clear()等效
		cout<<"clear()之后Mymap大小为:"<<Mymap.size()<<endl;
	}
}



原文地址:https://www.cnblogs.com/f8master/p/3826064.html