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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM