您的位置:首页 > 编程语言 > C语言/C++

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

2014-05-19 22:35 183 查看
1、综述

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

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

Map具有如下的功能特点:

[align=left]自动建立Key - value的对应。key 和 value可以是任意你需要的类型。 [/align]

[align=left]根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。[/align]

[align=left]快速插入Key - Value 记录。 [/align]

[align=left]快速删除记录 [/align]

[align=left]根据Key 修改value记录。 [/align]

[align=left]遍历所有记录。[/align]

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<<"\n找到key为11的项:"<<endl;
cout<< itr->first <<" "<<itr->second<<endl;
itr->second = "oneoneone";
cout<<"该项value修改为:"<<Mymap[11]<<endl;
}

Mymap.swap(Mymap2);//注意:交换的是两个map
cout<<"\n执行交换后:"<<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<<"\n删除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<<"\nMymap大小为:"<<Mymap.size()<<endl;
Mymap.clear();
//Mymap.erase(Mymap.begin(),Mymap.end());//与clear()等效
cout<<"clear()之后Mymap大小为:"<<Mymap.size()<<endl;
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Map 标准库 Container