C++:map用法

一、map基本用法


键值对

第一个参数为键的类型,第二个参数为值的类型。


  • 源代码
#include <iostream>
#include <string>
#include <map> using namespace std; int main() {
map<int,string> ::iterator iter; //迭代器iterator:变量iter的数据类型是由map<int,string>定义的iterator类型
map<int,string> myMap; //添加数据
myMap[1] = "one";
myMap[2] = "two";
myMap[3] = "three"; //遍历map
iter = myMap.begin(); //指向map首对元素
cout<<"myMap:"<<endl;
for (iter; iter != myMap.end(); iter++) { //myMap.end()指向map最后一对元素的后一对元素
cout << (*iter).first << " " << (*iter).second << "\n";
}
cout<<endl; //构造map
map<int, string> myMap2(myMap.begin(), myMap.end());//用myMap指定范围内的元素对,构造myMap2
map<int, string> myMap3(myMap);//用myMap构造myMap2 iter=myMap2.begin();
iter++;
cout<<"myMap2: "<<(*iter).first<<" " << (*iter).second<<endl<<endl; iter=myMap3.begin();
iter++;
iter++;
cout<<"myMap3: "<<(*iter).first<<" " << (*iter).second<<endl; return 0;
}


  • 运行结果:



二、map元素的默认值


当map内元素值为int类型或常量时,默认值为0。

当为String类型时,默认值不明,不显示


  1. map内元素值为int类型
#include <iostream>
#include <map>
using namespace std; int main(){
map<int,int> table; table[1]=1; cout<<table[0]<<endl;
cout<<table[1]<<endl;
return 0; }


  • 运行结果:




  1. map内元素值为常量类型
#include <iostream>
#include <map>
using namespace std; enum Symbols { //第一个枚举元素默认值为0,后续元素递增+1。
// 终结符号 Terminal symbols:TS
TS_I, // i
TS_PLUS, // +
TS_MULTIPLY, // *
TS_L_PARENS, // (
TS_R_PARENS, // )
TS_EOS, // #
TS_INVALID, // 非法字符 // 非终结符号 Non-terminal symbols:NS
NS_E, // E
NS_T, // T
NS_F // F
}; int main(){
map<int,enum Symbols> table; table[1]=TS_PLUS; cout<<table[0]<<endl;
cout<<table[1]<<endl; return 0; }


  • 运行结果:


  1. map内元素值为string类型
#include <iostream>
#include <map>
#include <string> using namespace std; int main(){
map<int,string> table; table[1]="abc"; cout<<table[0]<<endl;
cout<<table[1]<<endl;
return 0; }


  • 运行结果:

C++:map用法及元素的默认值的更多相关文章

  1. Java 创建数组的方式, 以及各种类型数组元素的默认值

    ①创建数组的方式3种 ①第1种方法 public class MyTest { public static void main(String[] args){ //method 1 int[] arr ...

  2. java各种数据类型的数组元素的默认值

    public class DataTypeDefaultValue { public static void main(String[] args) { // string类型数组的默认值null / ...

  3. (转)日期类型的input元素设置默认值为当天

    原文地址 html5的form元素对日期时间有丰富的支持 <input type="date"> <input type="time"> ...

  4. 日期类型的input元素设置默认值为当天

    html文件:<input name="" type="date" value="" id="datePicker" ...

  5. java 基本数据类型初始值(默认值)

    1.int类型定义的数组,初始化默认是0 2.String类型定义的数组,默认值是null 3.char类型定义的数组,默认值是0对应的字符 4.double类型定义的数组,默认值是0.0 5.flo ...

  6. SpringMVC中通过@ResponseBody返回对象,Js中调用@ResponseBody返回值,统计剩余评论字数的js,@RequestParam默认值,@PathVariable的用法

    1.SpringMVC中通过@ResponseBody.@RequestParam默认值,@PathVariable的用法 package com.kuman.cartoon.controller.f ...

  7. WPF中的常用布局 栈的实现 一个关于素数的神奇性质 C# defualt关键字默认值用法 接口通俗理解 C# Json序列化和反序列化 ASP.NET CORE系列【五】webapi整理以及RESTful风格化

    WPF中的常用布局   一 写在开头1.1 写在开头微软是一家伟大的公司.评价一门技术的好坏得看具体的需求,没有哪门技术是面面俱到地好,应该抛弃对微软和微软的技术的偏见. 1.2 本文内容本文主要内容 ...

  8. Newtonsoft.Json高级用法 1.忽略某些属性 2.默认值的处理 3.空值的处理 4.支持非公共成员 5.日期处理 6.自定义序列化的字段名称

    手机端应用讲究速度快,体验好.刚好手头上的一个项目服务端接口有性能问题,需要进行优化.在接口多次修改中,实体添加了很多字段用于中间计算或者存储,然后最终用Newtonsoft.Json进行序列化返回数 ...

  9. HTML元素margin、padding的默认值

    HTML元素margin.padding的默认值 element margin(单位像素) padding html 0 0 body 8 0 div 0 0 h1 21 0 h2 19 0 19 0 ...

随机推荐

  1. iptables 从入门到应用

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://powermichael.blog.51cto.com/12450987/1952 ...

  2. 工作日志,go get -v -x github.com/pebbe/zmq4 失败问题

    工作日志,go get -v -x github.com/pebbe/zmq4 失败问题 笔者因为工作需要使用ZeroMQ,但是在执行go get -v -x github.com/pebbe/zmq ...

  3. How to restore and recover a database from an RMAN backup. (Doc ID 881395.1)

    APPLIES TO: Oracle Database - Enterprise Edition - Version 10.1.0.2 to 11.2.0.2 [Release 10.1 to 11. ...

  4. React 以两种形式去创建组件 类或者函数(二)

    08==>创建组件以 1类的形式 或者以 2函数的形式 09==>使用组件 在src下创建components文件夹 是放组件的 CompType.js 组件 组件开头大写(重要) Com ...

  5. 201871010132-张潇潇《面向对象程序设计(java)》第一周学习总结

    面向对象程序设计(Java) 博文正文开头 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cn ...

  6. ubuntu下使用redshift开启护眼模式

    前面提到flux这东西在一些机器上并不能work,而且也找到了一些关于他不能work的线索(戳这里看原因).根据这些线索我们发现用flux不行了,得换用redshift,那好吧,我们就来装redshi ...

  7. SQL查询--索引

    索引概念和作用 索引是建立在表上的可选对象,目的是为了提高查询速度. 如果要在表中查询指定的记录,在没有索引的情况下,必须遍历整个表,而有了索引之后,只需要在索引中找到符合查询条件的索引字段值,就可以 ...

  8. 通过ES6 Module看import和require区别

    前言 说到import和require,大家平时开发中一定不少见,尤其是需要前端工程化的项目现在都已经离不开node了,在node环境下这两者都是大量存在的,大体上来说他们都是为了实现JS代码的模块化 ...

  9. Golang 入门 : channel(通道)

    笔者在<Golang 入门 : 竞争条件>一文中介绍了 Golang 并发编程中需要面对的竞争条件.本文我们就介绍如何使用 Golang 提供的 channel(通道) 消除竞争条件. C ...

  10. Spring案例--打印机

    目录: 1.applicationContext.xml配置文件 <?xml version="1.0" encoding="UTF-8"?> &l ...