对于修改C++指定key的value,网上查了很多,都说直接insert就会覆盖原来的值,是否是这样的呢?

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 
// mapmodifykey.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    map<string, int> m_map;
    m_map.insert(make_pair());
    m_map.insert(make_pair());
    m_map.insert(make_pair());
    m_map.insert(make_pair());
    map<string, int>::const_iterator iteMap = m_map.begin();
    cout << "==============旧值=============" << endl;
    for(; iteMap != m_map.end(); ++ iteMap)
    {
        cout << iteMap->first;
        cout << ":";
        cout << iteMap->second << endl;
    }
    m_map.insert(make_pair());
    //m_map["Kobe Bryant"] = 24;
    iteMap = m_map.begin();
    cout << "==============新值=============" << endl;
    for(; iteMap != m_map.end(); ++ iteMap)
    {
        cout << iteMap->first;
        cout << ":";
        cout << iteMap->second << endl;
    }
    ;
}

  看了半天,似乎并没有把key为Kobe Bryant的value修改为,还是之前的值。通过insert操作修改map指定key的value是不行的,正确的做法是这样的:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
// mapmodifykey.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>

using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    map<string, int> m_map;
    m_map.insert(make_pair());
    m_map.insert(make_pair());
    m_map.insert(make_pair());
    m_map.insert(make_pair());
    map<string, int>::const_iterator iteMap = m_map.begin();
    cout << "==============旧值=============" << endl;
    for(; iteMap != m_map.end(); ++ iteMap)
    {
        cout << iteMap->first;
        cout << ":";
        cout << iteMap->second << endl;
    }

//m_map.insert(make_pair("Kobe Bryant", 24));
;
    iteMap = m_map.begin();
    cout << "==============新值=============" << endl;
    for(; iteMap != m_map.end(); ++ iteMap)
    {
        cout << iteMap->first;
        cout << ":";
        cout << iteMap->second << endl;
    }
    ;
}

C++ map修改指定key的value的更多相关文章

  1. 修改Map中确定key对应的value问题

    今天在码代码的时候出现一个没有预料的问题: 先看下面的代码: public static void main(String[] args) { String[] files=new String[]{ ...

  2. 高效率遍历Map以及在循环过程中移除 remove指定key

    //高效率遍历Map以及在循环过程中移除 remove指定key //使用iter循环的时候 可以在循环中移除key,for在循环的过程中移除会报错哦 //本方法效率高 Iterator iter = ...

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

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

  4. hadoop 指定 key value分隔符

    原文:http://wingmzy.iteye.com/blog/1260570 hadoop中的map-reduce是处理<key,value>这样的键值对,故指定<key,val ...

  5. C++ STL中Map的按Key排序跟按Value排序

    C++ STL中Map的按Key排序和按Value排序 map是用来存放<key, value>键值对的数据结构,可以很方便快速的根据key查到相应的value.假如存储学生和其成绩(假定 ...

  6. SQLServer之修改FOREIGN KEY约束

    使用SSMS数据库管理工具修改FOREIGN KEY约束 1.连接数据库,选择数据表->右键点击->选择设计(或者展开键,选择要修改的外键,右键点击,选择修改,后面修改步骤相同). 2.在 ...

  7. Redis:解决分布式高并发修改同一个Key的问题

    本篇文章是通过watch(监控)+mutil(事务)实现应用于在分布式高并发处理等相关场景.下边先通过redis-cli.exe来测试多个线程修改时,遇到问题及解决问题. 高并发下修改同一个key遇到 ...

  8. C++ STL中Map的按Key排序

    为了实现快速查找,map内部本身就是按序存储的(比如红黑树).在我们插入<key, value>键值对时,就会按照key的大小顺序进行存储.这也是作为key的类型必须能够进行<运算比 ...

  9. map泛型 map不指定泛型 与 Map<Object,Object>的区别

    map泛型 map不指定泛型 与 Map<Object,Object>的区别 private void viewDetail(){ Map map1 = new HashMap(); Ma ...

随机推荐

  1. sql各种连接详解

      迁移时间:2017年6月1日16:33:58 CreateTime--2016年9月14日11:19:00Author:Marydon sql各种连接详解 参考链接: http://www.jb5 ...

  2. 〖Android〗CM10.2编译错误解决

    错误1: hardware/samsung/exynos4/hal/libhdmi/SecHdmi/SecHdmiV4L2Utils.cpp: In function 'int android::hd ...

  3. 加速nginx: 开启gzip

    nginx 是一个高性能的 Web 服务器,之前也写过一些关于 nginx 的文章.为了提高博客的响应速度,可以从设置 nginx 的 gzip 和缓存这2方面入手.为字体开启 gzip 和缓存能大大 ...

  4. Zoie中文文档及简单解析

    https://linkedin.jira.com/wiki/pages/viewpage.action?pageId=4456480 一.总体架构 Zoie是一个实时的搜索引擎系统,其需要逻辑上独立 ...

  5. MVC之Ajax.BeginForm使用详解之更新列表 mvc验证jquery.unobtrusive-ajax

    MVC之Ajax.BeginForm使用详解之更新列表   1.首先,请在配置文件设置如下:(该项默认都存在且为true) <add key="UnobtrusiveJavaScrip ...

  6. [Unity3D]Unity+Android交互教程——让手机&quot;动&quot;起来

    想要用Unity实现一个二维码扫描的功能,然后网上找插件,找到一个貌似叫EasyCodeScanner,但下载下来用用.真不好使,一导入执行就报错.调好错了再执行发现点button没反应.重复试了几遍 ...

  7. Leetcode Copy List with Random Pointer(面试题推荐)

    给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...

  8. Atitit.软件仪表盘(7)--温度监测子系统--电脑重要部件温度与监控and警报

    Atitit.软件仪表盘(7)--温度监测子系统--电脑重要部件温度与监控and警报 Cpu温度,风扇转速 主板温度 显卡温度 硬盘温度 电池温度 鲁大师  硬盘温度 Cpu温度  core temp ...

  9. Atitit.js javascript异常处理机制与java异常的转换.js exception process Voae

    Atitit.js javascript异常处理机制与java异常的转换.js exception processVoae 1. 1. javascript异常处理机制 1 2. 2. Web前后台异 ...

  10. 每日英语:China's Bad Earth

    In Dapu, a rain-drenched rural outpost in the heart of China's grain basket, a farmer grows crops th ...