1 map的本质

(1)关联式容器,键值对应

(2)增加和删除节点对迭代器的影响很小。

(3)对于迭代器来说不可以修改键值,只能修改对应的实值。

(4)map内部数据的祖居是自建一颗红黑树(或者说是平衡二叉树),具有自动排序的功能。

2 map的查增删

(1)map的插入

 #include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
//方式一 pair
map<int,string> mapStudent;
mapStudent.insert(pair<int,string>(1,"lan"));
mapStudent.insert(pair<int,string>(2,"ji"));
mapStudent.insert(pair<int,string>(1,"kjh"));
map<int,string>::iterator iter;
map<int,string>::iterator iter1;
//方式二
map<int string> mapStudent1;
mapStudent1.insert(map<int,string>::value_type(1,"nihao"));
mapStudent1.insert(map<int,string>::value_type(1,"ben"));
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
for(iter=mapStudent1.begin();iter!=mapStudent1.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl; }
return 0;
}

2 map的遍历

 #include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
map<int,string> mapStudent;
mapStudent[1]="stu_one";
mapStudent[1]="stu_two";
mapStudent[1]="stu_three";
map<int,string>::iterator iter = mapStudent.find(1);
if(iter!=mapStudent.end())
{
cout<<"找到了 value="<<iter->second<<endl;
}else
{
cout<<"没有找到"<<endl;
}
return 0;
}

3 map的删除

 #include <map>
#include <string>
#include <iostream>
using namespace std; int main()
{
map<int,string> mapStudent;
mapStudent[1]="stu_one";
mapStudent[1]="stu_two";
mapStudent[1]="stu_three";
map<int,string>::iterator iter = mapStudent.begin();
for(;iter!=mapStudent.end();)
{
if((*iter).sencond=="stu_one")
{
mapStudent.erase(iter++);//iter被erase以后就会失效 所以后面不能再有iter++
}else
{
++iter;
}
}
for(iter=mapStudent1.begin();iter!=mapStudent1.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl; }
return 0;
}

4 map排序

默认按照key从小到大。从大到小greater 相反less

 #include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<string,int,greater<string>> mapStudent;
mapStudent['nisan']=90;
mapStudent['nisan']=70;
mapStudent['nisan']=80;
map<string,int>::iterator iter = mapStudent.begin();
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}

自定义排序方式

 #include <string>
#include <iostream>
using namespace std;
struct CmpByKeyLength
{
bool operator()(const string &k1,const string&k2){
return k1.length()<k2.length();
}
};
int main()
{
map<string,int,CmpByKeyLength>mapStudent;
mapStudent['nisan']=90;
mapStudent['nisan']=70;
mapStudent['nisan']=80;
map<string,int>::iterator iter = mapStudent.begin();
for(iter=mapStudent.begin();iter!=mapStudent.end();iter++)
{
cout<<iter->first<<" "<<iter->second<<endl;
}
return 0;
}

------>加油美好的一天

c++中stl----map的更多相关文章

  1. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  2. stl中的map数据类型

    1.1 STL map 1.1.1 背景 关联容器使用键(key)来存储访问读取元素,而顺序容器则通过元素在容器中的位置存储和访问元素. 常见的顺序容器有:vector.list.deque.stac ...

  3. STL中关于map和set的四个问题?

    STL map和set的使用虽不复杂,但也有一些不易理解的地方,如: 为何map和set的插入删除效率比用其他序列容器高? 或许有得人能回答出来大概原因,但要彻底明白,还需要了解STL的底层数据结构. ...

  4. STL 中的map 与 hash_map的理解

    可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...

  5. STL中的map和unordered_map

    STL中的map和unordered_map map 头文件:#include 原理:std::map的内部实现了一颗红黑树,有对其键值进行排序的功能,所以map是一个有序的容器,map中的每一个元素 ...

  6. STL中的map和hash_map

    以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...

  7. stl::map之const函数访问

    如何在const成员数中访问stl::map呢?例如如下代码: string ConfigFileManager::MapQueryItem(const string& name) const ...

  8. hdu4941 Magical Forest (stl map)

    2014多校7最水的题   Magical Forest Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit ...

  9. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  10. STL map 用法

    首先make_pair Pairs C++标准程序库中凡是"必须返回两个值"的函数, 也都会利用pair对象  class pair可以将两个值视为一个单元.容器类别map和mul ...

随机推荐

  1. selector的button选中处理问题

    1.背景介绍 在做Android项目开发的时候,有时我们须要对button做一些特殊的处理,比方button点击的时候会有一个动画的效果,实际上就是几张图片在短时间的切换.再比方有时候我们须要对界面的 ...

  2. 【Sprint3冲刺之前】项目完成时间表

    Sprint2冲刺成果——项目完成时间表 项目完成时间表 经过Sprint2小组总结会议的探讨,我们决定一起约定项目alpha版本,beta版本,release版本的发布时间,发布形式及预计完成效果 ...

  3. UUID随机字符串

    public static void main(String[] args){ System.out.println(UUID.randomUUID().toString()); } //输出:698 ...

  4. (八):构建WineLib DLL

    (一):介绍 出于某些原因,你可能会发现你想要和使用Windows DLL一样使用你的Linux库.对于这有一些原因例如以下: 你正在支持一个使用多个第三方库的大应用.该项目在Linux中是可用的,可 ...

  5. pandas-事例练习

    补充: DataFrame.dropna(axis=0, how='any', thresh=None, subset=None, inplace=False) 功能:根据各标签的值中是否存在缺失数据 ...

  6. 对canvas arc()中counterclockwise参数的一些误解

    一直没有很细心地去研究CanvasRenderingContext2D对象的arc方法,对它的认识比较模糊,导致犯了一些错误,特发此文,以纠正之前的错误理解. arc()方法定义如下: arc() 方 ...

  7. 改动UITextfield的Placeholder字体的颜色

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/ ...

  8. React Native组件解析(二)之Text

    React Native组件解析(二)之Text 1. 概述 Text组件对应于iOS的UILabel,Android的TextView,用来显示文本.但是Text组件的内部使用的并不是flexbox ...

  9. Eclipse中连接Sql Sever2008 -----转自Yogurshine

    Eclipse中连接Sql Sever2008 -----转自Yogurshine 一 SQl Sever服务器配置 1我之前已经安装好SQL Sever 2008R2.(注意:安装一遍未成功时,一定 ...

  10. Android 属性动画ObjectAnimator和ValueAnimator讲解

    区别: ObjectAnimator 是直接对某个view进行更改. ValueAnimator 根据 TimeInterpolator 在不断产生相应的数据,来传进view  ,view自己做改变. ...