概述 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可以称为该关键字的值)的数据处理能力. 什么是一对一的数据映射。比如一个班级中,每个学生的学号跟他的姓名就存在着一一映射的关系,这个模型用map可能轻易描述,很明显学号用int描述,姓名用字符串描述.

map<int, string> mapStudent;

1. map的构造函数

 map<int, string> maphai;map<char,int> maphai;

2. 数据的插入
在构造map容器后,我们就可以往里面插入数据了。这里讲三种插入数据的方法:

  第一种:用insert函数插入pair数据,
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{map<int, string> mapStudent;mapStudent.insert(pair<int, string>(1, "sudent_one"));mapStudent.insert(pair<int, string>(2, "sudent_two"));mapStudent.insert(pair<int, string>(3, "sudent_three"));map<int, string> :: iterator iter;for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++){cout << iter->first << "\t" << iter->second << endl;}return 0;
}

第二种:用insert函数插入value_type数据,下面举例说明
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{map<int, string> mapStudent; mapStudent.insert(map<int, string> :: value_type(1, "student_one"));mapStudent.insert(map<int, string> :: value_type(2, "student_two"));mapStudent.insert(map<int, string> :: value_type(3, "student_three"));  map<int, string> :: iterator iter;for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++){cout << iter->first << "\t" << iter->second << endl;}return 0;
}
 第三种:用数组方式插入数据,下面举例说明
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{map<int, string> mapStudent;mapStudent[1] = "student_one";mapStudent[2] = "student_two";mapStudent[3] = "stident_three";map<int, string> :: iterator iter;for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++){cout << iter->first << "\t" << iter->second << endl;}system("pause");return 0;
}
 以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert 函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map 中有这个关键字时,insert 操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明mapStudent.insert(map<int, string>::va lue_type (1, “student_one ”));mapStudent.insert(map<int, string>::va lue_type (1, “student_two”));上面这两条语句执行后,map 中1 这个关键字对应的值是“student_one ”,第二条语句并没有生效.

map 的大小

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{map<int, string> mapStudent;mapStudent.insert(pair<int, string>(1, "sudent_one"));mapStudent.insert(pair<int, string>(2, "sudent_two"));mapStudent.insert(pair<int, string>(3, "sudent_three"));int len=mapStudent.size();cout<<len<<endl;return 0;
}

数据的遍历

第一种:应用前向迭代器
第二种:应用反相迭代器
#include <map>
#include <string>
#include <iostrea m>
Using namespace std;
Int ma in()
{Map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, “student_one ”));
mapStudent.insert(pair<int, string>(2, “student_two”));
mapStudent.insert(pair<int, string>(3, “student_three”));
map<int, string>::reverse_ iterator iter;
for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{Cout<<iter->first<< ” ”<<iter->second<<end;
}
}
第三种:用数组方式
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{map<int, string> mapStudent;mapStudent.insert(pair<int, string>(0, "student_zero ")); //得加这行,数组从0开始
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
int nSize = mapStudent.size();
for(int nIndex = 0;nIndex < nSize; nIndex++) //此法遍历仅限下标为数组,其实已从map退化成了数组,不具普遍性
{cout<<mapStudent[nIndex]<<endl;
}
}

数据的查找(包括判定这个关键字是否在map 中出现)

第一种:用count 函数来判定关键字是否出现
    其缺点是无法定位数据出现位置,由于map 的特性,一对一的映射关系,就决定了count 函数的返回值只有两个,要么是0,要么是1,出现的情况,当然是返回1 了
第二种:用find 函数来定位数据出现位置,

它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map 中没有要查找的数据,它返回的迭代器等于end 函数返回的迭代器,程序说明:

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1, "student_one"));
mapStudent.insert(pair<int, string>(2, "student_two"));
mapStudent.insert(pair<int, string>(3, "student_three"));
map<int, string>::iterator iter;
iter = mapStudent.find(1);
if(iter != mapStudent.end())
{cout<<"Find, the value is "<<iter->second<<endl;
} else
{cout<<"Do not Find "<<endl;
}
}

C++ with STL(二)map相关推荐

  1. c++ map是有序还是无序的_C++ STL中Map的按Key排序和按Value排序

    map是用来存放键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行存储就是个不错的选择. 我们这样定义 ...

  2. STL之map中排序方式的重载

    map中的数据默认是按key值字典序排的 栗子: #include <iostream> #include <map> using namespace std; //typed ...

  3. C++ STL中Map的按Key排序和按Value排序

    map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定不存在重名,当然可以对重名加以区分),我们用map来进行 ...

  4. STL之Map和MFC之CMap比较学习

    在MFC中,有CMap相关的map类, 在 map中有map. // Maps (aka Dictionaries)      class CMapWordToOb;         // map f ...

  5. stl中map函数_map :: empty()函数以及C ++ STL中的Example

    stl中map函数 C ++ STL映射:: empty() (C++ STL map::empty()) It is built-in function in C++ STL and used to ...

  6. STL 中map的用法详解

    STL 中map的用法详解 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可 ...

  7. stl中map函数_map :: max_size()函数,以及C ++ STL中的Example

    stl中map函数 C ++ STL映射:: max_size() (C++ STL map::max_size() ) It returns the maximum number of elemen ...

  8. 转 STL hash_map map

    几句话道出map和hash_map的区别 1. STL map is an associative array where keys are stored in sorted order using ...

  9. 集合 (二) ----- Map集合详解

    相关文章: <集合 (一) ----- 集合的基本概念与Collection集合详解> <集合 (二) ----- Map集合详解> 文章目录 Map集合详解 一.Map集合基 ...

  10. C++ STL容器 —— map/multimap 用法详解

    C++ STL容器 -- map/multimap 用法详解 写在前面:近期正在学习C++的STL容器,因此在这里做一下日志记录,主要介绍一些容器基本成员函数的用法, 配上实际用例,并不涉及原理.但别 ...

最新文章

  1. django模板的导入
  2. 学习人必看!空军老兵自学编程,仅隔一年成为国土安全部的数据库分析师
  3. html src加载外部静态资源,前端性能优化2:静态资源加载与优化
  4. python之蓝图blueprint浅析
  5. 处理过拟合问题-Regularization
  6. GMM_example(1)
  7. array_merge与array+array的区别
  8. 基于Xml 的IOC 容器-载入<list>的子元素
  9. 有理有据!为什么String选择数字31作为hashCode方法乘子?
  10. 读取oracle bfile字段,ORACLE中BFILE字段的使用研究_oracle
  11. 二分图判断(交叉染色)
  12. 不继承父类的某个属性_javascript中class的继承的基础用法
  13. c++中double类型控制小数位数
  14. C++ lambda表达式 std::function 深层详解
  15. opencv3.4.x和opencv4.x中 cv2.findContours的不同 ValueError: too many values to unpack (expected 2)
  16. 常见物理性能测试仪器设备档案
  17. JAVA经典算法题目
  18. 一台手机第一天支付1元,第二天支付2元,第三天支付4元,连续支付30天,请问一共需要支付多少钱?
  19. Redundant Variable 多余变量
  20. java 动态表单设计

热门文章

  1. soft_argmax
  2. Flask入门之Jinjia模板的一些语法
  3. 青龙羊毛——内容改版
  4. 你需要知道的MySQL开源存储引擎TokuDB
  5. 十三、limit 的使用
  6. android输出log,Android开发 Release情况下也能输出log
  7. android okhttp 架构,Android okhttp3.0 框架使用总结
  8. java 模拟鼠标键盘_使用SWT模拟鼠标键盘事件
  9. 链接时undefined reference to构造函数
  10. springboot获取resources路径_Docker构建SpringBoot应用