码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++-STL-map:map插入元素的几种方式【用数组方式插入数据】【用insert函数插入pair数据】【用insert函数插入value_type数据】


    一、第一种:用insert函数插入pair数据

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. map<int, string> mapStudent;
    8. mapStudent.insert(pair<int, string>(1, "student_one"));
    9. mapStudent.insert(pair<int, string>(2, "student_two"));
    10. mapStudent.insert(pair<int, string>(3, "student_three"));
    11. map<int, string>::iterator iter;
    12. for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    13. {
    14. cout<first<<" "<second<
    15. }
    16. }

    第二种:用insert函数插入value_type数据

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. Int main()
    6. {
    7. map<int, string> mapStudent;
    8. mapStudent.insert(map<int, string>::value_type (1, "student_one"));
    9. mapStudent.insert(map<int, string>::value_type (2, "student_two"));
    10. mapStudent.insert(map<int, string>::value_type (3, "student_three"));
    11. map<int, string>::iterator iter;
    12. for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    13. {
    14. cout<first<<" "<second<
    15. }
    16. }

    第三种:用数组方式插入数据

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. map<int, string> mapStudent;
    8. mapStudent[1] = "student_one";
    9. mapStudent[2] = "student_two";
    10. mapStudent[3] = "student_three";
    11. map<int, string>::iterator iter;
    12. for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    13. {
    14. cout<first<<" "<second<
    15. }
    16. }

    以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,
    但是用数组方式就不同了,它可以覆盖以前该关键字对应的值,用程序说明:

    1. mapStudent.insert(map<int, string>::value_type (1, "student_one"));
    2. mapStudent.insert(map<int, string>::value_type (1, "student_two"));

    上面这两条语句执行后,map中1这个关键字对应的值是“student_one”,第二条语句并没有生效,那么这就涉及到我们怎么知道insert语句是否插入成功的问题了,可以用pair来获得是否插入成功,程序如下
     

    1. pairint, string>::iterator, bool> Insert_Pair;
    2. Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));

    我们通过pair的第二个变量来知道是否插入成功,它的第一个变量返回的是一个map的迭代器,如果插入成功的话Insert_Pair.second应该是true的,否则为false。 下面给出完成代码,演示插入成功与否问题:

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. map<int, string> mapStudent;
    8. pairint, string>::iterator, bool> Insert_Pair;
    9. Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one"));
    10. if(Insert_Pair.second == true)
    11. {
    12. cout<<"Insert Successfully"<
    13. }
    14. else
    15. {
    16. cout<<"Insert Failure"<
    17. }
    18. Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two"));
    19. if(Insert_Pair.second == true)
    20. {
    21. cout<<"Insert Successfully"<
    22. }
    23. else
    24. {
    25. cout<<"Insert Failure"<
    26. }
    27. map<int, string>::iterator iter;
    28. for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    29. {
    30. cout<first<<" "<second<
    31. }
    32. }

    大家可以用如下程序,看下用数组插入在数据覆盖上的效果

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. map<int, string> mapStudent;
    8. mapStudent[1] = "student_one";
    9. mapStudent[1] = "student_two";
    10. mapStudent[2] = "student_three";
    11. map<int, string>::iterator iter;
    12. for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    13. {
    14. cout<first<<" "<second<
    15. }
    16. }

    C++ map的三种不同插入元素方法_zpznba的博客-CSDN博客_c++ map 添加元素

    c++ map容器的元素插入_高凌霄的博客-CSDN博客_c++ map添加元素

    C++map容器插入数据的4种方法_LaugustusJ的博客-CSDN博客_c++ map添加元素的方法

  • 相关阅读:
    这几个Python实战项目,让大家了解到它的神奇
    高级进阶班1——补充(利用平凡解优化流程、通过记录结构找到可能性划分的边界情况、动态规划填表的斜率优化技巧、上中位数结构)
    java版直播商城平台规划及常见的营销模式有哪些?
    GCC -fno-builtin & 内建函数
    Spring 更简单的读取和存储对象
    Django模版层
    HIL 测试
    我要写整个中文互联网界最牛逼的JVM系列教程 | 「类加载子系统」章节:ClassLoader的常用方法及其获取方法
    vue3 + vite 性能优化 ( 从5s -> 0.5s )
    时序预测 | MATLAB实现TCN时间卷积神经网络的时间序列预测
  • 原文地址:https://blog.csdn.net/u013250861/article/details/128089049
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号