map

map篇(红黑树,键值不许重复)

 头文件 :#include<map>

1.创建元素

View Code
1  map<string,int>v;//string 键值 int 映照数据

2.插入元素

View Code
1  v["a"]=1;
2  v["b"]=2;
3  v["a"]=1;//无效

3.元素输出(前向遍历)

View Code
1  map<string,int>::iterator  it;
2  for(it=v.begin();it!=v.end();it++)
3      cout<<(*it).first<<(*it).second<<endl;
4   map<string,int>::reverse_iterator  rit;//反向遍历
5    for(rit=v.rbegin();rit!=v.rend();rit++)
6      cout<<(*rit).first<<(*rit).second<<endl;

 

4.同set类似
5.常用函数的说明

格式: (函数的调用形式)  //(注释)

                v.empty()          //如果map为空则返回true
               v.count(temp)    // 返回temp出现的次数
               v.clear()            //删除所有元素
               v.size()           //返回map中元素的个数
               map<string,int,mycmp> //可以自定义比较函数

 

 

posted on 2012-07-25 20:24  L_S_X  阅读(139)  评论(0编辑  收藏  举报

导航