1. map的构造函数
Map<int, string> mapStudent;
2. 数据的插入
在构造 map容器后
第一种:用insert函数插入pair数据
#pragma warning (disable:4786) )
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->se cond<<end;
}
}
第二种:用insert函数插入value_type数据,下面举例说明
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce 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<<” ”<<iter->se cond<<end;
}
}
第三种:用数组方式插入数据,下面举例说明
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce std;
Int main()
{
Map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[2] = “student_two”;
mapStudent[3] = “student_three”;
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
Cout<<iter->first<<” ”<<iter->se cond<<end;
}
}
以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当 map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明
mapStudent.insert( map<int, string>::value_type (1, “student_one”));
mapStudent.insert( map<int, string>::value_type (1, “student_two”));
上面这两条语句执行后, map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
Pair< map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert( map<int, string>::value_type (1, “student_one”));
我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个 map的迭代器,如果插入成功的话Insert_Pair.se cond应该是true的,否则为false。
下面给出完成代码,演示插入成功与否问题
#in clude < map>
#in clude <string>
#in clude <iostream>
Using namespa ce std;
Int main()
{
Map<int, string> mapStudent;
Pair< map<int, string>::iterator, bool> Insert_Pair;
Insert_Pair = mapStudent.insert(pair<int, string>(1, “student_one”));
If(Insert_Pair.se cond == true)
{
Cout<<”Insert Su ccessfully”<<endl;
}
Else
{
Cout<<”Insert Failure”<<endl;
}

map以及类似指针iterator相关推荐

  1. Java知识点04——集合(Set、List、Queue、Map、Collection和Iterator、Collections工具类)

    Java知识点04--集合(Set.List.Queue.Map.Collection.Iterator.Collections工具类) 一.集合 1.1 集合概述 二.Collection 2.1 ...

  2. 重学JavaSE —— Map、Set、Iterator(迭代器) 简单笔记

    前言 本文是学习笔记,也有配套源码实例,主要是针对本人对Map.Set.Iterator进行的简单学习和笔记总结. 本文源代码已上传至Gitee:https://gitee.com/da-ji/ful ...

  3. Java容器(List、Map、Set、Iterator)

    容器是一个Java 所编写的程序,原先必须自行编写程序以管理对象关系,现在容器都会自动帮您做好. List特点:元素有放入顺序,元素可重复 Set特点:元素无放入顺序,元素不可重复(注意:元素虽然无放 ...

  4. map 值为指针_Go sync.Map 并发效率为什么那么高?

    点击上方蓝色"后端开发杂谈"关注我们, 专注于后端日常开发技术分享 Go sync.Map揭秘 简介: 对于熟悉 Go 语言的同学都知道, Go 自身的 map 是不支持并发读写, ...

  5. 努力前端【LeetCode-10】448. 找到所有数组中消失的数字 442. 数组中重复的数据(中等) 41. 缺失的第一个正数(困难) [鸽笼原理,数组,Map,类似No.645]

    文章目录 题目描述-448 一.哈希Map 二.空间复杂度的优化--鸽笼原理 三.总结 题目描述-442 一.还是hashMap 二.继续鸽笼原理 题目描述-41 一.基础方案 二.数组模拟Map 题 ...

  6. c++文件读取、容器(vector、map)、迭代(iterator)、排序(sort)综合案例

    1. 案例一 获取数据,处理数据是工作中必不可少的,本题通过少量数据主要考察大家对STL容器和迭代器,排序算法,以及文件操作等相关知识. 编程实现以下功能: 1.t1.txt文件中保存某柜台一年中不同 ...

  7. 【Smart_Point】动态内存与智能指针实战:文本查询程序(设计set,map,智能指针的应用)

    文章目录 Cpp读入结构性数组 文本查询程序 文本查询程序本版1 Cpp读入结构性数组 #include<sstream> #include<iostream> #includ ...

  8. Qt的json对象不具备类似指针、引用的行为导致的更新不成功问题解决

    如下代码: QJsonArray actionsArray; QJsonObject totalRootJson; totalRootJson.insert("actionsArray&qu ...

  9. c++ 的map、iterator用法

    https://blog.csdn.net/bangdingshouji/article/details/73028424 参考: 资料一:http://www.cplusplus.com/refer ...

最新文章

  1. jgit查询远程仓库_JAVA 使用jgit管理git仓库
  2. Winform开发框架重构总结
  3. 收集19个前端开发人员的必备工具
  4. 数据结构与算法--第一个只出现一次的字符
  5. html css做网页总结,学习CSS制作网页总结的一些经验
  6. 电脑时代计算机应用,【2017年整理】计算机应用与发展的神话时代.docx
  7. Android类参考---Fragment(五)
  8. 如何用Html+css3写一个简单的网页
  9. mybatis基础总结02 -配置详解
  10. 打开容器世界的大门: Docker、POD 初探
  11. github注册以及安装教程
  12. 关于Eclipse配置Tomcat8的问题
  13. Linux 创建并且运行Django项目
  14. 使用VS2019将c#生成dll文件
  15. Linux基础8-TCP的面向链接(三次四次)
  16. C语言 —— 多维数组
  17. Handler简单介绍
  18. 数据库查找姓李的人_数据库基本查询方法等
  19. STM32系列(HAL库)——F103C8T6通过MFRC522、RFID射频卡、门禁卡模块读取卡片ID
  20. 【js学习笔记-071】--- 浏览器和屏幕信息

热门文章

  1. Android中的约束布局
  2. ++和--操作符分析
  3. 树莓派怎么安装linux软件源,修改树莓派软件源
  4. wp转shp_【收藏】空间数据格式转换方法
  5. python私有方法应用场景_Python私有属性私有方法应用实例解析
  6. android 之Fragment(轻量级的Activity)详解
  7. c++ 纯虚函数和抽象类那些事(三)
  8. python executemany
  9. 101. Leetcode 139. 单词拆分 (动态规划-完全背包)
  10. Leetcode 面试题 01.01. 判定字符是否唯一 (每日一题 20211012)