c++map容器介绍

 Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!

  

1、map简介

map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。

对于迭代器来说,可以修改实值,而不能修改key。

2、map的功能

自动建立Key - value的对应。key 和 value可以是任意你需要的类型。

根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。

快速插入Key -Value 记录。

快速删除记录

根据Key 修改value记录。

遍历所有记录。

3、使用map

使用map得包含map类所在的头文件

#include <map>  //注意,STL头文件没有扩展名.h

map对象是模板类,需要关键字和存储对象两个模板参数:

std:map<int,string> personnel;

这样就定义了一个用int作为索引,并拥有相关联的指向string的指针.

为了使用方便,可以对模板类进行一下类型定义,

typedef map<int,CString> UDT_MAP_INT_CSTRING;

UDT_MAP_INT_CSTRING enumMap;

4、在map中插入元素

改变map中的条目非常简单,因为map类已经对[]操作符进行了重载

enumMap[1] ="One";

enumMap[2] ="Two";

.....

这样非常直观,但存在一个性能的问题。插入2时,先在enumMap中查找主键为2的项,没发现,然后将一个新的对象插入enumMap,键是2,

值是一个空字符串,插入完成后,将字符串赋为"Two";该方法会将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。

我们可以用以下方法来避免开销:

  enumMap.insert(map<int,CString> :: value_type(2,"Two"))

  enumMap.insert(pair<int,string>(102,"aclive"));

5、查找并获取map中的元素

下标操作符给出了获得一个值的最简单方法:

CString tmp =enumMap[2];

但是,只有当map中有这个键的实例时才对,否则会自动插入一个实例,值为初始化值。

我们可以使用Find()和Count()方法来发现一个键是否存在。

查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,

分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.

int nFindKey= 2;//要查找的Key

//定义一个条目变量(实际是指针)

UDT_MAP_INT_CSTRING::iterator it=enumMap.find(nFindKey);

if(it== enumMap.end()) {

//没找到

}

else {

//找到

}

通过map对象的方法获取的iterator数据类型是一个std::pair对象,包括两个数据 iterator->first和 iterator->second分别代表关键字和存储的数据

 6.从map中删除元素

移除某个map中某个条目用erase()

该成员方法的定义如下:

iterator erase(iterator it);//通过一个条目对象删除

iterator erase(iterator first,iterator last)//删除一个范围

size_type erase(const Key&key);//通过关键字删除

clear()就相当于enumMap.erase(enumMap.begin(),enumMap.end());

7.map中的swap用法

Map中德swap不是一个容器中的元素交换,而是两个容器的交换;

8.map中的sort问题

Map中的元素是自动按Key升序排序,所以不能对map用sort函数;

9.  map的基本操作函数:

      C++ Maps是一种关联式容器,包含“关键字/值”对

     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的函数

10.map的内存占用

map的数据结构为一颗红黑树,
该树的一个节点在不保存数据时,占用16字节的空间,
包括一个父节点指针,左右孩子指针,还有一个枚举值(标示红黑的,相当于平衡二叉树中的平衡因子),
可见,map还是很耗内存的。

 以下是map容器的应用实例:

#include <map>
#include <string> 
#include <iostream> 
using namespace std;
 
void map_insert(map < string, string > *mapStudent, string index, string x)
{ 
mapStudent->insert(map < string, string >::value_type(index, x));
}
 
int main(int argc, char **argv) 
{ 
char tmp[32] = ""; 
map < string, string > mapS;
 
//insert element 
map_insert(&mapS, "192.168.0.128", "xiong");
map_insert(&mapS, "192.168.200.3", "feng");
map_insert(&mapS, "192.168.200.33", "xiongfeng");
 
map < string, string >::iterator iter;
 
cout << "We Have Third Element:" << endl; 
cout << "-----------------------------" << endl;
 
//find element 
iter = mapS.find("192.168.0.33"); 
if (iter != mapS.end()) { 
cout << "find the elememt" << endl; 
cout << "It is:" << iter->second << endl; 
} else { 
cout << "not find the element" << endl; 
}
 
//see element 
for (iter = mapS.begin(); iter != mapS.end(); iter ) {
 
cout << "| " << iter->first << " | " << iter-> 
second << " |" << endl;
 
} 
cout << "-----------------------------" << endl;
 
map_insert(&mapS, "192.168.30.23", "xf");
 
cout << "After We Insert One Element:" << endl; 
cout << "-----------------------------" << endl; 
for (iter = mapS.begin(); iter != mapS.end(); iter ) {
 
cout << "| " << iter->first << " | " << iter-> 
second << " |" << endl; 
}
 
cout << "-----------------------------" << endl;
 
//delete element 
iter = mapS.find("192.168.200.33"); 
if (iter != mapS.end()) { 
cout << "find the element:" << iter->first << endl; 
cout << "delete element:" << iter->first << endl; 
cout << "=================================" << endl; 
mapS.erase(iter); 
} else { 
cout << "not find the element" << endl; 
} 
for (iter = mapS.begin(); iter != mapS.end(); iter ) {
 
cout << "| " << iter->first << " | " << iter-> 
second << " |" << endl;
 
} 
cout << "=================================" << endl;
 
return 0; 
} 

---------------------

map<string,int> m_map;

  1. m_map.insert(map<string,int>::value_type("hello",5));
  2. m_map.insert(make_pair("hello",5));

也就是说,insert后面的数据是pair类型或者是value_type类型了,然而对C++有了解的人都明白,其实value_type和pair<const k,v>是等价的、insert()中的参数必须是value_type类型,那么为什么insert()中的参数能够使用make_pair产生的pair<k,v>呢?

其实,因为我们在加入pair<k,v>时的k已经是常量了,所以可以加入。。。而正常来讲这都是所有编译器所能接受的。

在insert插入的同时,还有返回值来说明是否插入成功,就是pair< map<string,int>::iterator,bool> >类型,如本实例pair< map<string,int>::iterator,bool> > rent= m_map.insert(make_pair("hello",5));

rent->second即是成功与否的标志;rent->first就是返回的map<string,int>::iterator迭代器;rent->first.first就是string类型的数据。

正常的map默认按照key值排序,而map又没有像vector一样的sort()函数,那么如果将map按照value值排序呢,方法如下

方法. 将map中的key和value分别存放在一个pair类型的vector中,然后利用vector的sort函数排序:
 

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
bool cmp(const pair<int,int> &p1,const pair<int,int> &p2)//要用常数,不然编译错误 
{
    return p1.second<p2.second;
}
int main(void)  
{  
   map<int,int> mp;
   mp[1]=4;
   mp[2]=3;
   mp[3]=2;
   mp[4]=1;
   vector<pair<int,int> > arr;
   for (map<int,int>::iterator it=mp.begin();it!=mp.end();++it)
   {
       cout<<it->first<<'\t'<<it->second<<endl;
       arr.push_back(make_pair(it->first,it->second));
   }
   sort(arr.begin(),arr.end(),cmp);
   for (vector<pair<int,int> >::iterator it=arr.begin();it!=arr.end();++it)
   {
       cout<<it->first<<'\t'<<it->second<<endl;
   }
   return 0;  
}  

--------------------- 
http://blog.sina.com.cn/s/blog_63a9d08801011hl7.html
https://blog.csdn.net/qq_26399665/article/details/52969352 

https://blog.csdn.net/chao_xun/article/details/8037678 
 

猜你喜欢

转载自blog.csdn.net/m0_37154839/article/details/85265813