本文主要简单介绍C++中STL的关联容器map的基本使用方法,本人能力有限,如果文中有不当之处,欢迎指正,非常感谢!
1 map(映射):STL中的一种关联容器。
头文件:<map>
容器描述:由{键,值}对组成的集合,以某种作用于键值对上的谓词排列。
case(案例):手机电话薄里的姓名与电话号码的关联(唯一的电话号码(键)对应联系人姓名(值))。
2 map的构造
STL中对map有多种构造类型,以下仅列出最常用的构造类型。
map<string, int>mapname;
string表示键类型为string,int表示对应的值的类型,mapname为构造map的自定义名字。
3 数据的插入(实例)
现在我们来构造一个map,并命名为student,其中键为同学的名字,与键关联的值为该学生的分数。
本文全部的实例都以此例为基础,下文使用的student代表定义的map名称,name代表学生姓名,数字代表学生分数。
基本插入方式:
(1)student.insert(pair<string,int>("name",90));
(2) student.insert(map<string,int>::value_type("name",80));
注意:如果键已经存在,那么这两种插入方式将都不能插入;
但是对于student["name1"]=80;
student["name1"]=90;那么这时,name1的成绩就将从80修改为90;
实例代码:
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string,int>student;
map<string,int>::iterator it;
pair<map<string,int>::iterator,bool>error;
student["Li ming"]=80;
//二种基本插入方式
student.insert(pair<string,int>("Li lei",90));
student.insert(map<string,int>::value_type("Lucy",90));
for(it=student.begin();it!=student.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
//该插入方式体现键值的唯一性,即当键值已经存在时无法插入
student.insert(pair<string,int>("Li lei",90));
error=student.insert(pair<string,int>("Li ming",90));
if(error.second==false)
{
cout<<"Li ming had already matched"<<endl;
}
//这种插入方式也体现键值的唯一性,当键值已经存在时无法插入
student.insert(map<string,int>::value_type("Lucy",90));
error=student.insert(map<string,int>::value_type("Lucy",95));
if(error.second==false)
{
cout<<"Lucy had already matched"<<endl;
}
//注意这种构建联系时,如果键值已经存在,那么仍将修改键值所对应的值
//这时,Li lei的成绩将从90修改为100,这也说明了键值只能对应一个值
student["Li lei"]=100;
for(it=student.begin();it!=student.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
运行图片:
(3)map的一些技巧型插入:
a .在指定插入位置之后插入:
it=student.begain();
student.insert(it,pair<string,int>("name,80"));
it代表插入位置,如上面的it就代表的是容器的第一对元素,那么再插入的新元素就排在了第一个元素之后;
b. 从map A插入插入map B的元素:(通过实例展示)
#include "StdAfx.h"
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
map<string,int>student;
map<string,int>::iterator it;
student["Bill"]=100;
student["Li ming"]=80;
//在指定位置插入
it=student.begin();
student.insert(it,pair<string,int>("Li lei",100));
it++;
student.insert(it,pair<string,int>("Han mei",89));
for(it=student.begin();it!=student.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
cout<<"插入student中的元素,从第一个开始直到遇到“Li lei”结束"<<endl;
//在一个map中插入另一个map的(指定范围内的)元素
map<string,int>student2;
student2.insert(student.begin(),student.find("Li lei"));
for(it=student2.begin();it!=student2.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
运行结果:
注:该程序在VC环境下不能编译通过,请在VS上测试。
4 数据的查找
数据的查找用student.find("name");结果返回元素所在位置;
#include<string>
#include<map>
using namespace std;
int main()
{
map<string,int>student;
map<string,int>::iterator it;
student["Bill"]=100;
student["Li ming"]=80;
cout<<student.find("Bill")->second<<endl;
cout<<student.find("Li ming")->second<<endl;
return 0;
}
运行结果就是查找学生姓名对应的成绩。
5 数据的删除
采用student.erase(it);
student 自定义的map名,it 代表要删除的元素;
典型应用例题:
http://www.cnblogs.com/heat-man/archive/2013/01/11/2857050.html
参考资料:【c plus plus】