zoukankan      html  css  js  c++  java
  • C++ Map

    1.Map是什么

         今天我们来学习一下Map容易,Map是一种关联是容器,在map中增加和删除元素非常容易。可以修改一个特定的节点而不对其他节点不产生影响,由于map是一种关联式容器,Key--value结构我们必须明确,在map中我们可以根据key的值修改其对应的value的值,而不能修改key的值,其最大的特点是key和value的值可以是任意类型(

    内部类型你或用户自己定义的类型)。

    2. Map的特点:

           (1)快速建立key--value的索引。key和value可以是任意类型。

           (2)map的查询时间复杂度为LogN,也就是如果包含1000个记录,查询次数最多为10次,1,000,000个记录,最多查找20次。

           (3)快速插入Key - Value 记录。

           (4)快速删除记录

           (5)根据Key 修改value记录。

           (6)遍历所有记录。

    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"))

    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()

    该成员方法的定义如下

    1. iterator erase(iterator it); //通过一个条目对象删除
    2. iterator erase(iterator first, iterator last); //删除一个范围
    3. size_type erase(const Key& key); //通过关键字删除

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

    C++ STL map的使用

    以下是对C++中STL map的插入,查找,遍历及删除的例子:

     1 #include <map> 
     2 #include <string> 
     3 #include <iostream> 
     4 using namespace std; 
     5 
     6 void map_insert(map < string, string > *mapStudent, string index, string x) 
     7 { 
     8 mapStudent->insert(map < string, string >::value_type(index, x)); 
     9 } 
    10 
    11 int main(int argc, char **argv) 
    12 { 
    13 char tmp[32] = ""; 
    14 map < string, string > mapS; 
    15 
    16 //insert element 
    17 map_insert(&mapS, "192.168.0.128", "xiong"); 
    18 map_insert(&mapS, "192.168.200.3", "feng"); 
    19 map_insert(&mapS, "192.168.200.33", "xiongfeng"); 
    20 
    21 map < string, string >::iterator iter; 
    22 
    23 cout << "We Have Third Element:" << endl; 
    24 cout << "-----------------------------" << endl; 
    25 
    26 //find element 
    27 iter = mapS.find("192.168.0.33"); 
    28 if (iter != mapS.end()) { 
    29 cout << "find the elememt" << endl; 
    30 cout << "It is:" << iter->second << endl; 
    31 } else { 
    32 cout << "not find the element" << endl; 
    33 } 
    34 
    35 //see element 
    36 for (iter = mapS.begin(); iter != mapS.end(); iter ) { 
    37 
    38 cout << "| " << iter->first << " | " << iter-> 
    39 second << " |" << endl; 
    40 
    41 } 
    42 cout << "-----------------------------" << endl; 
    43 
    44 map_insert(&mapS, "192.168.30.23", "xf"); 
    45 
    46 cout << "After We Insert One Element:" << endl; 
    47 cout << "-----------------------------" << endl; 
    48 for (iter = mapS.begin(); iter != mapS.end(); iter ) { 
    49 
    50 cout << "| " << iter->first << " | " << iter-> 
    51 second << " |" << endl;
    52 
    53 } 
    54 
    55 cout << "-----------------------------" << endl; 
    56 
    57 //delete element 
    58 iter = mapS.find("192.168.200.33"); 
    59 if (iter != mapS.end()) { 
    60 cout << "find the element:" << iter->first << endl; 
    61 cout << "delete element:" << iter->first << endl; 
    62 cout << "=================================" << endl; 
    63 mapS.erase(iter); 
    64 } else { 
    65 cout << "not find the element" << endl; 
    66 } 
    67 for (iter = mapS.begin(); iter != mapS.end(); iter ) { 
    68 
    69 cout << "| " << iter->first << " | " << iter-> 
    70 second << " |" << endl; 
    71 
    72 } 
    73 cout << "=================================" << endl; 
    74 
    75 return 0; 
    76 }
  • 相关阅读:
    Ubuntu下iperf的安装
    frp内网穿透协助内网程序(如微信相关)开发
    Uncaught Error: Call to undefined function mcrypt_get_iv_size() 解决办法
    CentOS安装了iRedMail停用Amavisd + ClamAV + SpamAssassin
    申请Let’s Encrypt通配符HTTPS证书(certbot ACME v2版)
    关于ThinkPHP在Nginx服务器下因PATH_INFO出错的解决方法
    本地开发环境伪装成SSL连接的实现
    Web基础之日志
    Linux基础之防火墙
    面向接口及单例工厂随笔
  • 原文地址:https://www.cnblogs.com/7code/p/5050677.html
Copyright © 2011-2022 走看看