对于修改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. Ubuntu开机自动禁用无线网络

    让ubuntu开机自动禁用无线网络. 1.自启动脚本 将下面这条禁用无线网络的命令添加到“启动应用程序“中,这样开机时无线网络就会被自动禁用. dbus-send --system --type=me ...

  2. 【LeetCode】85. Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  3. 基于Redis的消息队列php-resque

    转载:http://netstu.5iunix.net/archives/201305-835/ 最近的做一个短信群发的项目,需要用到消息队列.因此开始了我对消息队列选型的漫长路. 为什么选型会纠结呢 ...

  4. Tomcat中部署Java Web应用程序的方式

    Tomcat中部署Java Web应用程序的几种方式: #PetWeb是工程名 1.在TOMCAT_HOME\conf\server.xml文件的HOST节点中加入 <Context docBa ...

  5. Linux-软件包管理-rpm命令管理-校验、文件提取

    rpm -V httpd 查看已安装的apache包中文件信息是否已经被人修改 rpm -ql httpd 查看已安装的apache包中文件的位置 vim /etc/httpd/conf/httpd. ...

  6. java反射详解及说明

    首先写一个Person类: package lltse.base.reflectdemo; public class Person { private String name ="张三&qu ...

  7. centos gnome桌面放大

    我不知道gnome 为什么要这么做.但是真的有效: gsettings set org.gnome.desktop.interface scaling-factor # 放大2倍

  8. vim的窗口切换

    当用vim写代码的时候,我喜欢一边看着头文件中结构的定义,一边编写实现的代码,这样就经常用到多窗口来编辑,查看文档. 1.同时打开多个文件,并横向排列 vim -o t.c t.h 2.同时打开多个文 ...

  9. 修改mysql中的auto_increment

    在mysql数据库中,如何修改自增值auto_increment呢?请看下面的语句: 1.sql语句 ALTER TABLE table_name AUTO_INCREMENT=1 2截断表,trun ...

  10. RSA加密的测试demo

    使用.net自带的RSA,需要引用System.Security.Cryptography 测试环境.net4.6 static void Main(string[] args) { var RSA ...