MAP赋值和插入,对于相同ID的处理方式不同,前者为替换 后者为插入失败

#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<int, string> mapStudent;
pair<map<int, string>::iterator, bool> Insert_Pair;
mapStudent[1] = "student_one";
mapStudent[1] = "student_one2"; cout << "====================MAP 赋值测试=====================\n" <<endl ;
map<int, string>::iterator iter;
for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<< endl;
} cout << "====================MAP INSERT测试=====================\n" <<endl ;
Insert_Pair = mapStudent.insert(pair<int, string>(2, "student_2"));
if(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
}
Insert_Pair = mapStudent.insert(pair<int, string>(2, "student_2222"));
if(Insert_Pair.second == true)
{
cout<<"Insert Successfully"<<endl;
}
else
{
cout<<"Insert Failure"<<endl;
} for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
cout<<iter->first<<" "<<iter->second<< endl;
}
}
 
 

语艺杂谈1 – MAP赋值与插入的更多相关文章

  1. go语言基础之map赋值、遍历、删除 、做函数参数

    1.map赋值 示例: package main //必须有个main包 import "fmt" func main() { m1 := map[int]string{1: &q ...

  2. map容器对象插入数据的4种方式

    #include <string> #include <iostream>  #include <map>  #include <utility>  u ...

  3. map赋值前要先初始化:assignment to entry in nil map

    注意这种map的嵌套的形式,make只初始化了map[string]T部分(T为map[int]int),所以下面的赋值会出现错误: test := make(map[string]map[int]i ...

  4. map两种插入方法解析(insert() 与 下标[]操作)

    insert 含义是: 如果key存在,则插入失败,如果key不存在,就创建这个key-value. 实例: map.insert((key, value)) 利用下标操作的含义是: 如果这个key存 ...

  5. GO语言中json与map的转换

    直接上代码(需要引入encoding/json包) // 当前程序的包名 package main // 导入其它的包 import ( "encoding/json" " ...

  6. c++ map和mutimaps 插入值

    (1)运用value_type std::map<std::string, float> col1; col1.insert(std::map<std::string,float&g ...

  7. Golang 嵌套map赋值办法

    http://my.oschina.net/sol/blog/159060 m := map[string]map[string]string{} mm, ok := m["kkk" ...

  8. C++ 关联容器之map插入相同键元素与查找元素操作

    一.插入相同键元素操作 (1)insert方法 在map中的键必须是唯一的,当想map中连续插入键相同但值不同的元素时,编译和运行时都不会发生任何错误,系统会忽略后面的对已存在的键的插入操作,如 ma ...

  9. Golang Map实现(四) map 的赋值和扩容

    title: Golang Map 实现 (四) date: 2020-04-28 18:20:30 tags: golang map 操作,是map 实现中较复杂的逻辑.因为当赋值时,为了减少has ...

随机推荐

  1. EntityFramework中支持BulkInsert扩展(转载)

    前言 很显然,你应该不至于使用 EntityFramework 直接插入 10W 数据到数据库中,那可能得用上个几分钟.EntityFramework 最被人诟病的地方就是它的性能,处理大量数据时的效 ...

  2. Linux 串口编程(转)

    无论那种操作方式,一般都通过四个步骤来完成: (1) 打开串口 (2) 配置串口 (3) 读写串口 (4) 关闭串口 转自

  3. Xamarin环境搭建

    Xamarin的环境搭建 Xamarin在osx下面的环境搭建包括开发者帐号申请,下载安装Xamarin以及Xcode,以及Xamarin Studio上编写第一个应用程序.Xamarin是一个跨平台 ...

  4. BeagleBone Black项目实训手册(大学霸内部资料)

    BeagleBone Black项目实训手册(大学霸内部资料) 介绍:本教程是<BeagleBone Black快速入门教程>的后续教程.本教程以项目操作为主,讲解LED项目.声音项目.传 ...

  5. HashMap两种遍历数据的方式

    HashMap的遍历有两种方式,一种是entrySet的方式,另外一种是keySet的方式. 第一种利用entrySet的方式: Map map = new HashMap(); Iterator i ...

  6. BZOJ4389 : ZYB and Trees

    Link-Cut Tree维护. 每个点x维护以下信息: v:这个点的点权 s:实链上的信息和 st:子树信息和(不包括链上) sa:子树+链上的信息和 as:所有虚儿子的sa的和 则有 s[x]=v ...

  7. Win 8 App开发框架解析

    开发前准备: Windows 8 RTM MSDN订阅用户下载地址: https://msdn.microsoft.com/zh-cn/subscriptions/securedownloads/hh ...

  8. NOIp 2006 作业调度方案 Label:坑 模拟(tyvj你不给我ac,我就把名字献给附中oj)

    福建师大附中链接:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1211 [问题描述] 我们现在要利用m台机器加工n个工件,每个工件都有m道工序 ...

  9. 编程中i++与++i的区别

  10. 【BZOJ】2178: 圆的面积并

    http://www.lydsy.com/JudgeOnline/problem.php?id=2178 题意:给出n<=1000个圆,求这些圆的面积并 #include <cstdio& ...