1、map简介
map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。
对于迭代器来说,可以修改实值,而不能修改key。
2、map的功能
自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
快速插入Key -Value 记录。
快速删除记录
根据Key 修改value记录。
遍历所有记录。
3、使用map
使用map得包含map类所在的头文件
#include <map> //注意,STL头文件没有扩展名.h
map对象是模板类,需要关键字和存储对象两个模板参数:
std:map<int,string> personnel;
这样就定义了一个用int作为索引,并拥有相关联的指向string的指针.
4. map的构造函数
map共提供了6个构造函数,这块涉及到内存分配器这些东西,略过不表,在下面我们将接触到一些map的构造方法,这里要说下的就是,我们通常用如下方法构造一个map:
map<int, string> mapStudent;
5、 数据的插入
在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法:
第一种:用insert函数插入pair数据
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std ;
int main()
{
map<int ,string > stu ;
map<int ,string >::iterator iter ;//前向迭代器;
map<int ,string >::reverse_iterator it ; // 反向迭代器
stu.insert(pair<int ,string>(1,"student_1"));
stu.insert(pair<int ,string>(2,"student_2"));
stu.insert(pair<int ,string>(3,"student_3"));
stu.insert(pair<int ,string>(4,"student_4"));
cout<<stu.size()<<endl; // map 大小
cout<<"前向迭代器 遍历map"<<endl;
for(iter = stu.begin() ;iter!=stu.end() ;iter++)
{
cout<<" key: " <<iter->first <<" value :"<<iter->second<<endl;
}
cout<<"反向迭代器 遍历map"<<endl;
for(it = stu.rbegin();it!=stu.rend() ;it++)
{
cout<<"key: " <<it->first <<"value :"<<it->second<<endl;
}
return 0 ;
}
第二种:用insert函数插入value_type数据,
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std ;
int main()
{
map<int ,string > stu ;
map<int ,string >::iterator iter ;//前向迭代器;
map<int ,string >::reverse_iterator it ; // 反向迭代器
stu.insert(map<int ,string >::value_type (1,"student_1"));
stu.insert(map<int ,string >::value_type (2,"student_2"));
stu.insert(map<int ,string >::value_type (3,"student_3"));
stu.insert(map<int ,string >::value_type (4,"student_4"));
stu.insert(map<int ,string >::value_type (1,"student_5")); // 注意这个没有插入map
/*
map 它具有集合的唯一性,即不能出现同样的键,即当map中有这个关键字时,insert操作是插入数据不了的,
*/
cout<<stu.size()<<endl;
cout<<"前向迭代器 遍历 map"<<endl;
for(iter = stu.begin() ;iter!=stu.end() ;iter++)
{
cout<<" key: " <<iter->first <<" value :"<<iter->second<<endl;
}
cout<<"反向迭代器 遍历 map"<<endl;
for(it = stu.rbegin();it!=stu.rend() ;it++)
{
cout<<"key: " <<it->first <<"value :"<<it->second<<endl;
}
return 0 ;
}
第三种 :直接采用下标法对map进行插入,覆盖
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std ;
int main()
{
map<int ,string > stu ;
map<int ,string >::iterator iter ;//前向迭代器;
map<int ,string >::reverse_iterator it ; // 反向迭代器
stu[1] = "student_1" ;
stu[2] = "student_2" ;
stu[3] = "student_3" ;
stu[4] = "student_4" ;
stu[1] = "student_5" ; // 注意这个用下标法可以更改;
/*
map 它具有集合的唯一性,即不能出现同样的键,即当map中有这个关键字时,insert操作是插入数据不了的,
*/
cout<<stu.size()<<endl;
cout<<"前向迭代器 遍历 map"<<endl;
for(iter = stu.begin() ;iter!=stu.end() ;iter++)
{
cout<<" key: " <<iter->first <<" value :"<<iter->second<<endl;
}
cout<<"反向迭代器 遍历 map"<<endl;
for(it = stu.rbegin();it!=stu.rend() ;it++)
{
cout<<"key: " <<it->first <<"value :"<<it->second<<endl;
}
return 0 ;
}
6. 查找并获取map中的元素(包括判定这个关键字是否在map中出现)
在这里我们将体会,map在数据插入时保证有序的好处。
要判定一个数据(关键字)是否在map中出现的方法比较多,这里标题虽然是数据的查找,在这里将穿插着大量的map基本用法。
这里给出三种数据查找方法
第一种:用count函数来判定关键字是否出现,其缺点是无法定位数据出现位置,由于map的特性,一对一的映射关系,就决定了count函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1了
第二种:用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器。
查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,
分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.
,
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std ;
int main()
{
map<int ,string > stu ;
map<int ,string >::iterator iter ;//前向迭代器;
map<int ,string >::reverse_iterator it ; // 反向迭代器
stu[1] = "student_1" ;
stu[2] = "student_2" ;
stu[3] = "student_3" ;
stu[4] = "student_4" ;
//stu[1] = "student_5" ; // 注意这个用下标法可以更改;
/*
map 它具有集合的唯一性,即不能出现同样的键,即当map中有这个关键字时,insert操作是插入数据不了的,
*/
iter = stu.find(5); // find()返回一个迭代器,当所要找的键 存在时返回该位置的迭代器,否则返回最后一个键的位置上的
// 迭代器
if(iter!=stu.end())
cout<<stu[2]<<endl;
else
{
cout<<"没有"<<endl;
stu.insert(pair<int ,string >(5,"student_5"));
}
iter = stu.begin() ;
while(iter!=stu.end())
{
cout<<iter->first <<" " <<iter->second<<endl;
/* iter -> first 表示键 */
/* iter -> second 表示值*/
iter++;
}
return 0 ;
}
从map中删除元素
移除某个map中某个条目用erase()
该成员方法的定义如下:
iterator erase(iterator it);//通过一个条目对象删除
iterator erase(iterator first,iterator last)//删除一个范围
size_type erase(const Key&key);//通过关键字删除
clear()就相当于enumMap.erase(enumMap.begin(),enumMap.end());
这里要用到erase函数,它有三个重载了的函数,下面在例子中详细说明它们的用法
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <map>
using namespace std ;
int main()
{
int m ;
map<int ,string > stu ;
map<int ,string >::iterator iter ;//前向迭代器;
map<int ,string >::reverse_iterator it ; // 反向迭代器
stu.insert(map<int ,string>::value_type (1,"student_1"));
stu.insert(map<int ,string>::value_type (2,"student_2"));
stu.insert(map<int ,string>::value_type (3,"student_3"));
stu.insert(map<int ,string>::value_type (4,"student_4"));
cout<<"输入要删除的"<<endl;
cin >> m ;
iter = stu.find(m);
if(iter!=stu.end())
{
stu.erase(iter);
}
else
{
cout<<"没有找到"<<endl;
}
iter = stu.begin() ;
while(iter!=stu.end())
{
cout<<iter->first<<" "<<iter->second<<endl;
iter++ ;
}
return 0 ;
}
7、
map的基本操作函数:
C++ maps是一种关联式容器,包含“关键字/值”对
begin() 返回指向map头部的迭代器
clear() 删除所有元素
count() 返回指定元素出现的次数
empty() 如果map为空则返回true
end() 返回指向map末尾的迭代器
equal_range() 返回特殊条目的迭代器对
erase() 删除一个元素
find() 查找一个元素
get_allocator() 返回map的配置器
insert() 插入元素
key_comp() 返回比较元素key的函数
lower_bound() 返回键值>=给定元素的第一个位置
max_size() 返回可以容纳的最大元素个数
rbegin() 返回一个指向map尾部的逆向迭代器
rend() 返回一个指向map头部的逆向迭代器
size() 返回map中元素的个数
swap() 交换两个map
upper_bound() 返回键值>给定元素的第一个位置
value_comp() 返回比较元素value的函数