zoukankan      html  css  js  c++  java
  • map表基本操作

    map表的操作一般是根据键值key进行操作,也可以用value值进行操作。不过建议用键值key操作,方便简单。另外,还有一种数据结构叫做集合set,集合只有一个键值key,同时key也是value。

    注:1、由于写的是一些简单的测试程序,所以以下代码的逻辑并不完整,函数的返回结果并未处理,另外函数实现的时候,函数的IF,else分支也并未处理,使用时需要稍加注意。

           2、以上提到的另外一种数据结构集合set的操作和map表类似,甚至可以说是照搬,只不过set只有键值key(同时也是value值)。

    MapStruct.h

     1 #pragma once
     2 #include <map>
     3 
     4 class MapStruct
     5 {
     6 public:
     7     MapStruct(void);
     8     ~MapStruct(void);
     9 
    10 public:
    11     void mapInit(void);//初始化map表,用数组方式插入数据
    12 
    13     bool mapInsert(int num, std::string& sInsertstring);//插入数据
    14 
    15     bool mapDelete(int num);//此处根据键值key删除数据,也可根据value值操作
    16 
    17     bool mapFind(int num);//map表查找,此处是根据键值key查找,也可用value值查找
    18 
    19     void mapPrint(void);//打印map表各项
    20 
    21 private:
    22     std::map<int, std::string>MapList;
    23 };

    MapStruct.cpp

     1 #include <iostream>
     2 #include "MapStruct.h"
     3 
     4 MapStruct::MapStruct(void)
     5 {
     6 }
     7 
     8 
     9 MapStruct::~MapStruct(void)
    10 {
    11 }
    12 
    13 void MapStruct::mapInit()
    14 {
    15     if (MapList.empty() )
    16     {
    17         MapList[1] = "student_one";
    18         MapList[2] = "student_two";
    19         MapList[3] = "student_three";
    20         MapList[4] = "student_four";
    21         MapList[5] = "student_five";
    22     }
    23 
    24 }
    25 
    26 bool MapStruct::mapInsert(int num, std::string& sInsertstring)
    27 {
    28     MapList[num] = sInsertstring;//数组方式插入
    29     //MapList.insert(std::map<int,std::string> :: value_type(num,sInsertstring) );//insert插入,建议用第一种
    30     return true;
    31 }
    32 
    33 bool MapStruct::mapDelete(int num)
    34 {
    35     std::map<int, std::string>::const_iterator ptr = MapList.find(num);
    36 
    37     if (ptr == MapList.end() )
    38     {
    39         std::cout<<"删除失败"<<std::endl;
    40         return false;
    41     }
    42 
    43     std::cout<<"删除成功"<<std::endl;
    44     MapList.erase(ptr);
    45 
    46     return  true;
    47 }
    48 bool MapStruct::mapFind(int num)
    49 {
    50     std::map<int, std::string>::const_iterator ptr = MapList.find(num);
    51 
    52     if (ptr == MapList.end() )
    53     {
    54         std::cout<<"没有查询到"<<std::endl;
    55         return false;
    56 
    57     }
    58 
    59     std::string _string = ptr->second;
    60     std::cout<<"查询到"<<_string.c_str()<<std::endl;
    61     return true;
    62 }
    63 
    64 void MapStruct::mapPrint()
    65 {
    66     std::map<int, std::string>::const_iterator ptr;
    67 
    68     for (ptr = MapList.begin(); ptr != MapList.end(); ptr++)
    69     {
    70         std::cout<<ptr->first<<":"<<ptr->second.c_str()<<std::endl;
    71     }
    72 }

    main.cpp

     1 #include <iostream>
     2 #include "MapStruct.h"
     3 
     4 int main()
     5 {
     6     MapStruct _MapStruct;
     7     std::cout<<"初始化"<<std::endl;
     8     _MapStruct.mapInit();
     9     _MapStruct.mapPrint();
    10 
    11     std::cout<<"插入"<<std::endl;
    12     std::string _string1 = "student_six";
    13     (void)_MapStruct.mapInsert(6,_string1);
    14     std::string _string2 = "student_seven";
    15     (void)_MapStruct.mapInsert(7,_string2);
    16     _MapStruct.mapPrint();
    17 
    18     std::cout<<"删除"<<std::endl;
    19     (void)_MapStruct.mapDelete(6);
    20     _MapStruct.mapPrint();
    21 
    22     std::cout<<"查找"<<std::endl;
    23     (void)_MapStruct.mapFind(3);
    24     return 0;
    25 }

    注:3、 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的函数

  • 相关阅读:
    Delphi中的接口和抽象类
    设计模式之六大原则
    C 标准库
    linux 管道和重定向
    linux c创建静态库(.a)
    一个C语言程序
    C#动态创建lambda表达式
    linq中order by 和group by (含lambda表达式实现)以及综合案例
    微信扫码登陆原理
    二维码扫码支付原理
  • 原文地址:https://www.cnblogs.com/LYF-LIUDAO/p/7083882.html
Copyright © 2011-2022 走看看