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")] = ;
strMap[string("jerry")] = ;
strMap[string("jason")] = ;
strMap[string("jimmy")] = ; pair<string,int> value(string("david"),);
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 = ; //可以通过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;
}
}

C++中map的用法的更多相关文章

  1. STL中map的用法

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

  2. C++11中map的用法

    最全的c++map的用法 1. map最基本的构造函数:map<string ,int>mapstring; map<int,string >mapint;map<sri ...

  3. C语言 · C++中map的用法详解

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

  4. [转]Java中Map的用法详解

    转载地址:http://www.zhixing123.cn/jsp/30113.html Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictio ...

  5. Java中Map的用法

    Map的一般用法 1.声明一个Map : Map map = new HashMap(); 2 .向map中放值 ,注意: map是key-value的形式存放的,如: map.put("s ...

  6. Java中Map的用法详解

    Map简介 将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值.此接口取代 Dictionary 类,后者完全是一个抽象类,而不是一个接口. Map 接口提供三种collecti ...

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

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

  8. C++中Map常见用法以及按照value排序

    今天做一个简单的算法题,居然用了1个小时,STL unordered_map用多了,没想到map这次派上了用场,这里记录一下: 算法题为 给一个字符串例如  abaaba,每连续两个字符组成一个子串 ...

  9. JS中Map的用法

    声明 var map = new Map(); 设值 map.set("key","value"); 取值 map.get("key"); ...

随机推荐

  1. 同时调整lv分区的大小(减少一个,增加另一个)

    author:headsen chen date: 2018-04-20  16:48:06 1.查看分区:/home 为67G,太大了,/ 是50g,太小了. [root@localhost ~]# ...

  2. Leetcode-Construct Binary Tree from inorder and postorder travesal

    Given inorder and postorder traversal of a tree, construct the binary tree. Solution: /** * Definiti ...

  3. 转SpringMVC Controller 返回值的可选类型

    spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void. ModelAndView @RequestMap ...

  4. CodeForces 667A Pouring Rain

    A. Pouring Rain time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. window.location.href = window.location.href window.location.reload()

    w 0-会议预订提交了预订日期,预订成功后默认显示仅显示当前日期的新页面若显示预定日的信息,则可以对预定日存入cookie: http://stackoverflow.com/questions/24 ...

  6. MFC DLL获取当前路径

    .首先定义此获取模块的静态方法 #if _MSC_VER >= 1300 // for VC 7.0 // from ATL 7.0 sources #ifndef _delayimp_h ex ...

  7. Python--(并发编程之线程Part2)

    GIL只能保证垃圾回收机制的安全,进程中的数据安全还是需要自定义锁 线程执行代码首先要抢到GIL全局锁,假设线程X首先抢到,以为要抢到自定义锁要执行代码,所以这个线程在执行代码的时候就很容抢到了自定义 ...

  8. Python 模块之 xlrd (Excel文件读写)

    # 1. 导入模块 import xlrd # 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFile.xls') # 3. 使用技巧 # 3.1 获 ...

  9. Linux学习笔记(6)磁盘分区(LVM)

    1.逻辑管理技术LVM的概念 1.1 LVM ,逻辑卷管理,以便扩展管理盘符. PV:物理卷 VG:卷组 LV:逻辑卷 PE(physical Extend):物理扩展(默认4M),就是我们逻辑卷管理 ...

  10. pycharm断点调试

    step over 执行下一步 蓝色高亮的那一行表示准备执行的代码