对于修改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. 基于Python Tornado的在线问答系统

    概述 本项目使用最新的Tornado开发.实现了在线提问,回答,评论等功能.使用到Tornado的generator,长轮询等等技术, 支持MySQL的异步连接. 详细 代码下载:http://www ...

  2. 【TP3.2与TP5.0区别】

    Tp3.2 和 Tp5.0之间的区别   5.0版本和之前版本的差异较大,本篇对熟悉3.2版本的用户给出了一些5.0的主要区别. URL和路由 5.0的URL访问不再支持普通URL模式,路由也不支持正 ...

  3. angularjs-1

    1.属性表达式 属性表达式计算是发生在作用域中的.Javascript默认是以window为作用域的.AngularJS要使用window作用域的话得用$window来指向全局window对象. 比如 ...

  4. Tsung运行环境安装(转)

    转自:http://www.cnblogs.com/tsbc/p/4272974.html#_Toc372013359 tsung Tsung运行环境安装 检查安装一下依赖包,以免在安装的时候报错.( ...

  5. EntityFramework Data Annotations

    详细的实体映射介绍(Data Annotation) http://msdn.microsoft.com/en-us/data/jj591583

  6. appium 1.6.3 + ios 10.2 + xcode 8.2.1 真机运行iphone app

    appium命令行启动: appium --address "127.0.0.1" --session-override --pre-launch --debug-log-spac ...

  7. mysql innodb的重要组件

    innodb包涵如下几个组件 一.innodb_buffer_pool: 1 它主要用来缓存数据与索引(准确的讲由于innodb中的表是由聚集索引组织的,所以数据只不是过主键这个索引的叶子结点). 二 ...

  8. Java中RunTime类介绍

    Runtime 类代表着Java程序的运行时环境,每个Java程序都有一个Runtime实例,该类会被自动创建,我们可以通过Runtime.getRuntime() 方法来获取当前程序的Runtime ...

  9. Android 使用PLDroidPlayer播放网络视频 依据视频角度自己主动旋转

    近期由于项目需求 .须要播放网络视频 .于是乎 研究了一番 ,说说我遇到的那些坑 如今市面上有几个比較主流好用的第三方框架 Vitamio ( 体积比較大,有商业化风险 github:https:// ...

  10. putty自带工具plink自动登陆ssh

    PLINK.EXE -C -N -D 127.0.0.1:7000 root@111.111.111.111  -pw 123456 解释成中文: PLINK.EXE -启用数据压缩 -不要shell ...