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. [Algorithms] Counting Sort

    Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed ra ...

  2. grep、egrep命令用法

    何谓正则表达式 正则表达式,又称正规表示法.常规表示法(Regular Expression,在代码中常简写为regex.regexp或RE),是一类字符所书写的模式,其中许多字符不表示其字面意义,而 ...

  3. Android 点击电话号码之间拨号

    点击电话号码之间拨打电话,可用通过下面的方式实现: 假设电话号码以TextView的方式显示 1.Intent方式 在TextView的响应事件中 : String phone = tvphone.g ...

  4. python array

    python中通常情况下for循环会枚举各个元素不会访问下标,例如: l = [1,2,4,6] for val in l: print l 但是有时候我们会需要在便利数组的同时访问下标,这时候可以借 ...

  5. js对象转成用&拼接的请求参数(转)

    var parseParam=function(param, key){ var paramStr=""; if(param instanceof String||param in ...

  6. 名义人均GDP的背后,中国真实的人均GDP是1.2万美元!(中国GDP含金量较高)

    来源:天涯社区 根据IMF(国际货币基金组织)在今年4月的报告,2014年份中国人均GDP为7600美元,在185个国家当中排行第78位. 然而,根据楼主在国外行走多年的经验,巴西.墨西哥.马来西亚. ...

  7. C#中的另类语法

    一..net中return的另类写法: 不知道是从3.5还是从4.0开始C#语法中的return有了新的写法示例如下: public string functionDemo() {       str ...

  8. python常见模块之collections模块

    一.模块简介 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict.namedtu ...

  9. Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法

    Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  10. 在MySQL数据库的表中可以给某个整数类型的字段赋字符串类型的值