map的特性是,所有元素都会根据元素的减值自动被排序。map的所有元素都是pair,同时拥有实值(value)和键值(key)。pair的第一个元素会被视为键值,第二个元素会被视为实值。map不允许两个元素拥有相同的键值。

下面看一下<stl_pair.h>中的pair定义:

template <class T1, class T2>

struct pair{

  typedef T1 first_type;

  typedef T2 second_type;

  T1 first;//注意,它是public

  T2 second;//注意,它是public

  pair() : first(T1()),second(T2()) {}

  pair(const T1&a,const T2&b) :first(a),second(b) {}

};

当客户端对map进行元素新增操作(insert)和删除(erase)时,操作之前的所有迭代器,在操作完成之后依然有效。被删除的迭代器除外。

标准的STL map是以红黑树为底层机制完成的,每一个节点的内容是一个pair。

一、map的基本构造函数

map<string , int >strMap;

map<int ,string >intMap;

map<sring, char>strMap;

map< char ,string>charMap;

map<char ,int>charMap;

map<int ,char >intMap;

二、map添加数据

map<int ,string> maplive;  
 1.pair<int,string> value(1,"a");maplive.insert(value);

等价于maplive.insert(pair<int,string>(1,"a"));

2. maplive.insert(map<int,string>::value_type(1,"a"));

3. maplive[1]="a";//map中最简单最常用的插入添加!

三、map的基本操作函数:

  begin()          返回指向map头部的迭代器
  clear()         删除所有元素
      count()          返回指定元素出现的次数
      empty()          如果map为空则返回true
      end()            返回指向map末尾的迭代器
      equal_range()    返回特殊条目的迭代器对
      erase()          删除一个元素
      find()           查找一个元素
      get_allocator()  返回map的配置器
      insert()         插入元素
      key_comp()       返回比较元素key的函数
      lower_bound()    返回键值>=给定元素的第一个位置
      max_size()       返回可以容纳的最大元素个数
      rbegin()         返回一个指向map尾部的逆向迭代器
      rend()           返回一个指向map头部的逆向迭代器
      size()           返回map中元素的个数
      swap()            交换两个map
      upper_bound()     返回键值>给定元素的第一个位置
      value_comp()      返回比较元素value的函数

#include <iostream>
#include "string.h"
#include "stdio.h"
#include<map>
using namespace std;int main(){map<string,int> strMap;  //以string为键值,以int为实值strMap[string("jjhou")] = 1;strMap[string("jerry")] = 2;strMap[string("jason")] = 3;strMap[string("jimmy")] = 4;pair<string,int> value(string("david"),5);strMap.insert(value);//插入新元素
map<string,int>::iterator strmap_iter = strMap.begin();for(;strmap_iter !=strMap.end();strmap_iter++){cout<<strmap_iter->first<<' '<<strmap_iter->second<<endl;}cout<<endl;int number = strMap[string("jjhou")];cout<<number<<endl;cout<<endl;//查找元素map<string,int>::iterator iter1;//面对关联式容器,应该使用其所提供的find函数来搜索元素,会比使用STL算法find()更有效率。因为STL算法find()只是循环搜索。iter1 = strMap.find(string("mchen"));if(iter1 == strMap.end())cout<<"mchen no fount"<<endl;cout<<endl;iter1 = strMap.find(string("jerry"));if(iter1 != strMap.end())cout<<"jerry fount"<<endl;cout<<endl;//修改实值,键值不可修改iter1->second = 9; //可以通过map迭代器修改“value”(not key)int number1 = strMap[string("jerry")];cout<<number1<<endl;//删除元素map<string,int>::iterator strmap_iter1 = strMap.begin();for(;strmap_iter1 !=strMap.end();strmap_iter1++){cout<<strmap_iter1->first<<' '<<strmap_iter1->second<<endl;}cout<<endl;strMap.erase(iter1);//删除一个条目strMap.erase(string("jason"));//根据键值删除
map<string,int>::iterator strmap_iter2 = strMap.begin();for(;strmap_iter2 !=strMap.end();strmap_iter2++){cout<<strmap_iter2->first<<' '<<strmap_iter2->second<<endl;}
}

转载于:https://www.cnblogs.com/omelet/p/6617362.html

C++中map的用法相关推荐

  1. STL 中map的用法详解

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

  2. linux 脚本map,shell中map的用法

    ##实例代码 #!/bin/bash cat ./switchsql.txt | while read line do tmp_partition_name=`echo $line | awk -F ...

  3. map函数作用c语言,C语言 · C++中map的用法详解

    转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义 (1) mapMap; (2) 或者是:typedef   mapMym ...

  4. C++中map的用法详解

    转载自:http://blog.csdn.net/sunquana/article/details/12576729 一.定义   (1) map<string,   int>   Map ...

  5. C++ 中 map 的用法

    C++ 中 map 是一种键值对容器 初始化并赋值 map<string, int> mapAge = {{"张三", 19}, {"李四", 18 ...

  6. Python中map()函数用法

    map() 是python的内置函数,会根据提供的函数对指定序列做映射. 对可迭代函数*iterables中的每个元素应用func方法,将结果作为迭代器对象返回. 注意:map()函数返回的是一个新的 ...

  7. leetcode 537 py 中map函数用法

    题目出自leetcode class Solution:def complexNumberMultiply(self, num1: str, num2: str) -> str:#(a0+b0i ...

  8. python中map的用法

    0 语法描述 map()会根据提供的函数对指定序列做映射. 语法: map(function, iterable, ...) 参数: function函数 iterable一个或多个序列 第一个参数f ...

  9. java中Map的用法(HaspMap用法)

    public interface Map<K,V> 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. 1 import java.util.HashMap; 2 ...

最新文章

  1. CTFshow php特性 web94
  2. Apache Camel Test Framework(MOCK)
  3. Openresty Nginx 负载均衡
  4. Java基础入门笔记-使用变量并打印
  5. 计算机辅助设计源程序,计算机辅助设计(插值法程序).doc
  6. excel表中怎么插入visio_Excel工作表中的排序,你真的掌握吗?10张动图带你了解!...
  7. 数据库容灾、复制解决方案全分析
  8. 使用VS开发基于Oracle程序的严重问题
  9. 外网live Meeting客户端无法连接到会议
  10. iconfont图标本地使用
  11. 基于Zigbee和LabView的无线温度采集系统
  12. FFmpeg+SDL纯视频播放器
  13. 计算机在投资审计中应用方法,计算机辅助审计技术在投资审计中的应用研究(原稿)_0...
  14. 怎么下载lce_icesword下载
  15. java为什么被开发者_Spring为何受到Java开发者的青睐?Spring是如何简化Java开发的?...
  16. Fulutter 设置圆角背景图片Container 设置边框、圆角、阴影
  17. 【Excel常用函数】VLookup函数使用教程,附视频教程
  18. 数据人需要掌握的技能,从底层到应用
  19. 多传感器分布式融合算法——多传感器网络协同目标跟踪和定位
  20. 共享汽车倒下了,为何我感觉心中的石头终于落地?

热门文章

  1. HTML数字比较大小游戏,Javascript 比较两个数大小并输出最大数
  2. sqlmap绕过d盾_WEBSHELL免杀绕过WAF思路amp;方法(一)
  3. Python中“is”和“==”的区别
  4. step在c语言中什么作用,C语言step-by-step(二)(数据类型)
  5. dbvisualizer查询mysql中文乱码_使用PLSQL Developer和DbVisualizer查询oracle数据库时出现乱码...
  6. 宝塔linux计划任务ftp,宝塔面板定时备份网站及数据库至FTP存储空间图文教程
  7. python神经网络风格_[Deep-Learning-with-Python]使用LSTM生成尼采风格文章
  8. ASP.NET MVC – 样式和布局简介
  9. STM32单片机真的落后?
  10. HDU2675(二分算法)