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. Druid-代码段-1-4

    所属文章:池化技术(一)Druid是如何管理数据库连接的? 本代码段对应流程1.3,连接可用性测试: //数据库连接可用性测试 protected boolean testConnectionInte ...

  2. DOS下查看驱动版本号

    1.进入目录:C:\Program Files\NVIDIA Corporation\NVISMI 2.输入命令nvidia-smi 可以看到我的显卡驱动版本号为431.60

  3. cf之 前缀和差分

    给定一个n×n的WB矩阵,给定一个k∗k的能把B变成W的橡皮擦,求橡皮擦作用一次后,全为W的行.列总数最大值 连接:http://codeforces.com/contest/1200/problem ...

  4. Java读写Excel文件,利用POI

    直接看工具类代码吧, package com.example.demo.util; import com.example.demo.entity.ExcelDataVO; import org.apa ...

  5. ES6 class类中定义私有变量

    ES6 class类中定义私有变量 class类的不足 看起来, es6 中 class 的出现拉近了 JS 和传统 OOP 语言的距离.但是,它仅仅是一个语法糖罢了,不能实现传统 OOP 语言一样的 ...

  6. CentOS下yum方式安装FFmpeg

    FFmpeg一个完整的跨平台解决方案,用于记录,转换和流式传输音频和视频. 文档:https://www.ffmpeg.org/documentation.html FFmpeg安装 1.安装Nux ...

  7. NLP中的数据增强

    相关方法合集见:https://github.com/quincyliang/nlp-data-augmentation 较为简单的数据增强的方法见论文:https://arxiv.org/pdf/1 ...

  8. 微信小程序开发练习

    微信小程序开发工具git管理 https://blog.csdn.net/qq_36672905/article/details/82887102 这个开发工具的界面和交互真的是熟悉又友好,吹爆他

  9. Linux 部署vue项目(使用nginx)

    1.部署Nginx 请参考Linux下部署nginx,此处不再重复 2.Vue项目打包 # 打包正式环境 npm run build:prod # 打包预发布环境 npm run build:stag ...

  10. PageHelper使用以及PageInfo中分页对象的转化

    在使用Mybatis查询数据库展示到前端的过程中不可避免的要考虑到分页问题,这时就引入了Mybatis的PageHelper插件,这个插件对分页功能进行了强有力的封装,只需要将查询出来的数据List集 ...