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. Model Vaildation

    https://docs.asp.net/en/latest/mvc/models/validation.html 许多有用的验证属性都必须引用命名空间: System.ComponentModel. ...

  2. linux 块设备驱动(四)——简单的sbull实例

    #include <linux/module.h> #include <linux/moduleparam.h> #include <linux/init.h> # ...

  3. 2018-11-9-匿名函数&递归函数初识

    1.匿名函数(lambda) 2.递归函数初识

  4. 阿里Java开发手册学习 2 异常日志

    异常日志 异常处理 1.不要捕获继承自RuntimeException的运行时异常类,这类异常通常由程序员来校验,来保证查询的健壮性. 2. 不要对大段代码 try catch,分清稳定代码和非稳定代 ...

  5. Boost智能指针——scoped_ptr

    boost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放. 上一段代码,以及其输出: #include <string ...

  6. CSS3学习笔记(4)—上下滑动展开的按钮

    最近写了一个动画,下面来看看我以前写的一个上下滑动展开的按钮效果: 这类的效果经常会在一些网站页面下载按钮处看到,当你鼠标悬浮在下载按钮时,会提醒你是否已注册,或者点击登录什么的小提示~~~~~ 一. ...

  7. Bestcoder BestCoder Round #28 A Missing number(查找缺失的合法数字)

    Problem Description There is a permutation without two numbers in it, and now you know what numbers ...

  8. hadoop集群异常问题总结

    1. Could not find or load main class java.library.path=.opt.hadoop.lib 我的环境上是 hadoopopts变量的配置问题,至于为啥 ...

  9. SQL Server中数据类型对应C#中数据类型

    在SQL Server 2008中新建数据表的时候有33种数据类型可选,下面分别列举了这些类型对应的C#数据类型 //----------------------------------------- ...

  10. 自定义带图标input样式

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...