C++  map用法

map 是一种关联容器,  提供一对一的关联, 关联的形式为: KEY----VALUE     关键字不重复。multimap与map类似,但是允许关键字重复
即:关键字和与之对应的值
关键字起到索引的作用, 在map中查找记录 就是根据关键字查找,关键字和值可以是任意类型
map 也可看做是关键字映射的集合即map中不可出现重复的关键字,每条映射的关键字都是不同的。          
map 是基于红黑树结构的,其查找时间为LOG(N)


 如:
    map<int, int >               //第一个为关键字,第二个为此关键字所对应的值     一个关键字只对应一个值, 是一对一的映射关系
    map<CString,int>
    map<int, CString>
    map<CString,CString>

头文件:
#include <map>
using namespace std; //必须加上


一map的定义
map对象是模板类,需要关键字和存储对象两个模板参数,基本的定义模式如下:
std:map<int, string> personnel;
这样就定义了一个以int为键,值为string的map对象personnel。
map中定义了以下三个类型:
map<K, V>::key_type : 表示map容器中,索引的类型;
map<K, V>::mapped_type : 表示map容器中,键所关联的值的类型;
map<K, V>::value_type : 表示一个pair类型,它的first元素具有const map<K, V>::key_type类型,而second元素则有map<K, V>::mapped_type类型
对迭代器进行解引用时,将获得一个引用,指向容器中一个value_type类型的值,对于map容器,其value_type是pair类型。


为了使用方便,可以对模板类进行一下类型定义,
typedef map<int, CString> UDT_MAP_INT_CSTRING; 
UDT_MAP_INT_CSTRING enumMap;




二 map的功能
自动建立Key - value的对应。key 和 value可以是任意你需要的类型,但是需要注意的是对于key的类型,唯一的约束就是必须支持<操作符。
根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
快速插入Key - Value 记录。
快速删除记录
根据Key 修改value记录。
遍历所有记录。


1.插入元素
1)insert 函数插入
map<int,int> idMap;  
idMap.insert(pair<int,int>(1,1));  
idMap.insert(map<int,int>::value_type(2,1));  
 用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的
 
 判断是否插入成功
map<int,int> idMap;  
idMap.insert(pair<int,int>(1,1));  
idMap.insert(map<int,int>::value_type(2,1));  
  
pair<map<int,int>::iterator,bool> InsertPair;  
  
InsertPair=idMap.insert (pair<int,int>(1,1));  
  
if (InsertPair.second==true)  
{  
    cout<<"insert successfully";  
}else  
    cout<<"insert failure";  


2).数组插入方式
map<int ,int> idMap;
idMap[1]=2;
用数组方式就不同了,它可以覆盖以前该关键字对应的值
但存在一个性能的问题。该方法会将每个插入值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。而用insert方法则可直接赋值为显示值。


例子:
    map<string, int> word_count; // 定义了一个空的map对象word_count;
word_count["Anna"] = 1;
分析:
 1.在word_count中查找键为Anna的元素,没有找到.
 2.将一个新的键-值对插入到word_count中,他的键是const string类型的对象,保存Anna。而他的值则采用直初始化,这就意味着在本例中指为0.
 3.将这个新的键-值对插入到word_count中
 4.读取新插入的元素,并将她的值赋为1.
使用下标访问map与使用下标访问数组或者vector的行为是截然不同的,
使用下标访问不存在的元素将导致在map容器中添加一个新的元素,他的键即为该下标值。




2.判断是否存在
1)size_type count(const Key&  _key)const    //map.count(k):返回map中键K的出现次数(对于map而言,由于一个key对应一个value,因此返回只有0和1,因此可以用此函数判断k是否在map中))
int num = idMa.count(1)   
if(num ==0)
{
cout<<"the key 1 does not exist";
}else{
cout<<"exist";
}
2)查找并获取map中的元素
使用下标获取元素存在一个很危险的副作用:如果该键不在map容器中,那么下标操作会插入一个具有该键的新元素。因此引入map对象的查询操作:
iterator find(const Key& _key );//map.find(k)返回map中指向键k的迭代器,如果不存在键k,则返回超出末端迭代器。


const_iterator find(const Key& _Key) const;


map<int,int>::iterator it;  
it=idMap.find(2);  
if (it==idMap.end())  
{  
    cout<<"can not find 2";  
}else{  
    int first=it->first;  
    int second=it->second;  
}  






3.从map中删除元素
该成员方法的定义如下:
iterator erase(iterator it);                      //通过一个条目对象删除
iterator erase(iterator first, iterator last);    //删除一个范围
size_type erase(const Key& key);                  //通过关键字删除


//迭代器删除  
map<int,int>::iterator it;  
it=idMap.find(1);  
idMap.erase(it);  
  
//关键字删除  
idMap.erase(1);  
  
//成片删除 或清空  
idMap.erase(idMap.begin (),idMap.end());  
  
//清空  
idMap.clear ();  






4.获得容器的大小
size_type size() const;
int nSize=idMap.size();




5.遍历


前向迭代器
map<int,int>::iterator it;  
for (it=idMap.begin ();it!=idMap.end();it++)  
{  
    cout<<it->first<<endl;  
    cout<<it->second<<endl;  



反向迭代器
map<int,int>::reverse_iterator iter;  
for (iter=idMap.rbegin ();iter!=idMap.rend ();iter++)  
{  
    cout<<iter->first<<endl;  
    cout<<iter->second<<endl;  
}  


6基本函数
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的函数  




7.扩展
1)iterator lower_bound(const Key& _Key);
const_iterator lower_bound(const Key& _Key) const;
第一个等于或大于Key的元素
2)
iterator upper_bound(const Key& _Key);
const_iterator upper_bound(const Key& _Key) const;
第一个大于Key的元素


3)
pair <const_iterator, const_iterator> equal_range (
   const Key& _Key
) const;
pair <iterator, iterator> equal_range (
   const Key& _Key
);
Equal_range函数返回一个pair,pair里面第一个变量是Lower_bound返回的迭代器,pair里面第二个迭代器是Upper_bound返回的迭代器,如果这两个迭代器相等的话,则说明map中不出现这个关键字






8.示例:
map<int,vector<int>>  m_DianmingMap;  


AddDianmingMap(int nXueqi,int nXuehao)  
{  
                    //将此学生添加到已点名容器中  
  
        map<int,vector<int>>::iterator it;  
        it=m_DianmingMap.find(nXueqi);  
        if (it==m_DianmingMap.end ()) //先查找关键字有无此学期ID  
        {  
             //容器中不存在 则添加  
            vector<int>  int_Vec;  
            int_Vec.push_back(nXuehao);  
  
            m_DianmingMap[nXueqi]=int_Vec;  
              
  
        }else{//在查找 此学期中 有无此学号ID  
  
            vector<int>::iterator itVec=find(it->second.begin (),it->second.end(),m_nXuehaoID);  
            if(itVec==it->second.end())  
            {  
                //没有此学生则添加  
                it->second.push_back(nXuehao);  
            }  
        }  
  
        return TRUE;  
  
}  













  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值