#include "map"   //引入头文件

初始化:

std::map <int, std::string> _map1;  //初始化
//c++11中引入的,可以直接在初始化时赋值
std::map <int, std::string> _map =
{
{0,"11"},
{2,"22"},
{3,"33"},
};

插入:

// 如果已经存在键值200,则会作赋值修改操作,如果没有则插入
_map[200] = "booomm";
//通过insert插入
_map.insert(std::pair<int, std::string>(4, "33333"));

取值:

用at和[]:

//Map中元素取值主要有at和[]两种操作,at会作下标检查,而[]不会。
std::cout<< _map.at(100).c_str()<< std::endl;//使用at会进行关键字检查,因为没有100因此该语句会报错
std::cout << _map.at(4).c_str() << std::endl;//因为已经有4了,不会报错 std::cout << _map[300].c_str() << std::endl;//ID_Name中没有关键字200,使用[]取值会导致插入,因此不会报错,但打印结果为空

用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器:

auto iter=_map.find(200);
if(iter==_map.end())
{
assert(false); //若没有200这个元素,会返回end
return;
}
auto value=iter->second; //second返回iter的value

遍历:

for(auto item =_map.begin();item!=_map.end();item++)
{
auto value= item->second;
if(value==L"11")
{
//do someting.....
}
}

参考资料:http://blog.csdn.net/shuzfan/article/details/53115922

C++ std::map用法简介的更多相关文章

  1. std::map用法

    STL是标准C++系统的一组模板类,使用STL模板类最大的好处就是在各种C++编译器上都通用.    在STL模板类中,用于线性数据存储管理的类主要有vector, list, map 等等.本文主要 ...

  2. C++ std::map::erase用法及其陷阱

    1.引入: STL的map中有一个erase方法用来从一个map中删除制定的节点 eg: map<string,string> mapTest; typedef map<string ...

  3. C++中的STL中map用法详解

    Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...

  4. C++中的STL中map用法详解(转)

    原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解   Map是STL的一个关联容器,它提供 ...

  5. UVA11995【I can guess the data structrue!!】【水】+UVA11991【map用法】

    先看UVA11995 两份代码一份直接用C写的,一份用STL写的 #include <iostream> #include <stdio.h> #include <str ...

  6. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  7. sort函数(cmp)、map用法---------------Tju_Oj_2312Help Me with the Game

    这道题里主要学习了sort函数.sort的cmp函数写法.C++的map用法(其实和数组一样) Your task is to read a picture of a chessboard posit ...

  8. SpringBoot系列之@PropertySource用法简介

    SpringBoot系列之@PropertySource用法简介 继上篇博客:SpringBoot系列之@Value和@ConfigurationProperties用法对比之后,本博客继续介绍一下@ ...

  9. C++:map用法及元素的默认值

    C++:map用法 一.map基本用法 键值对 第一个参数为键的类型,第二个参数为值的类型. 源代码 #include <iostream> #include <string> ...

随机推荐

  1. PostgreSQL问题解决--连接数过多

    I am trying to connect to a Postgresql database, I am getting the following Error: Error:org.postgre ...

  2. 扩展kmp板子

    using namespace std; #include <cstdio> #include <cstring> #include <algorithm> #de ...

  3. win10系统,vbox下安装centos6/7,挂载实现目录共享

    用下载好的iso文件,新建虚拟机(所有步骤默认下一步即可). 我用的centos版本:CentOS-7-x86_64-Minimal-1908.iso ISO下载地址 设置虚拟机(指定好镜像后,先不要 ...

  4. C#多线程实现方法——线程池(Thread Pool)

    ThreadPool使用 同步机制   ThreadPool使用 需要定义waitcallback委托形式如 public delegate void WaitCallback(object stat ...

  5. System.Web.Mvc.ValueProviderResult.cs

    ylbtech-System.Web.Mvc.ValueProviderResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral ...

  6. JDK源码阅读--LinkedList

    public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, D ...

  7. shell对文件,文件夹的操作

    x=$ OUTFILENAME="output_${x}.sql" if [ -f $OUTFILENAME ];then rm $OUTFILENAME fi 如果文件存在则删除 ...

  8. eclipse memory analyzer对系统内存溢出堆文件解析(转)

    本文转之:https://blog.csdn.net/rachel_luo/article/details/8992461 前言 性能分析工具之-- Eclipse Memory Analyzer t ...

  9. Python学习day42-数据库的基本操作(1)

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  10. [Turn]C# 强制关闭当前程序进程(完全Kill掉不留痕迹)

    C#代码 /// <summary> /// 运行DOS命令 /// DOS关闭进程命令(ntsd -c q -p PID )PID为进程的ID /// </summary> ...