对于修改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. Tomcat 改服务器编码(Java 修改字符串编码格式)

    对于客户端发来的汉字,我们一般需要转码: ------------------------------------------------------------------------------- ...

  2. 在where子句中经常使用的运算符

    比较运算符 >   <   <=   >=   =    <> 大于.小于.大于(小于)等于.不等于 BETWEEN  ...AND... 显示在某一区间的值 IN ...

  3. OAuth2.0官方文档中文翻译

    http://page.renren.com/699032478/note/708597990 (一)背景知识 OAuth 2.0很可能是下一代的“用户验证和授权”标准,目前在国内还没有很靠谱的技术资 ...

  4. 14条最佳JS代码编写技巧

    http://gaohaixian.blog.163.com/blog/static/123260105201142645458315/写任何编程代码,不同的开发者都会有不同的见解.但参考一下总是好的 ...

  5. 一个事件激活多个JavaScript函数

    http://www.cnblogs.com/meil/archive/2006/09/20/509359.html如果你的网页中一个“OnLoad”事件要激活两个以上的JavaScript函数,那怎 ...

  6. Linux命令-压缩解压命令:gzip、gunzip

    gzip [选项] 源文件名(压缩前) gunzip [选项] 源文件名(压缩后) cd /tmp 切换tmp目录 rm -rf * 强制删除tmp目录下面所有的文件和目录 touch beijing ...

  7. C#中byte类型转换为double类型

    // Initialize unmanged memory to hold the array. int size = Marshal.SizeOf(bytes[0]) * bytes.Length; ...

  8. spring 学习资料备份

    易百教程  https://www.yiibai.com/spring/spring-autowiring-by-name.html

  9. windows phone 切换多语言时,商店标题显示错误的问题

    前段时间,用业余时间写了一款 wp8 app(“超级滤镜”商店,中文地址:英文地址),在多语言的时候,给 app title 和 app tile title 进行多语言时(参考 MSDN),中文商店 ...

  10. dependent-name ‘xxx::yyy’ is parsed as a non-type, but instantiation yields a type

    简言之,就是说你该用typename的地方没用typename,如以下代码 template<class Cont> void frontInsertion(Cont& ci) { ...