[C++]STL中map的用法


大家好,有時候讀代碼,寫代碼用到了,講下map 。首先說下stl_map.h 中class map 很長。

原理:紅黑樹,對數據自動排序,可以做到一一數據映射關係。

map的構造函數:

map 作爲STL中一個關聯容器,提供一對一的數據處理能力,

1、有6個構造函數 比如

Map<int, string> mapStudent

2、數據的插入insert

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::iterator  iter;
       for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
{
       Cout<<iter->first<<”   ”<<iter->second<<end;
}
}
3、map的大小 size()

map<int, string> mapStudent;
int nSize = mapStudent.size();
4、數據的遍歷

(1)第一個就是上面的前向迭代器。

(2)反向迭代器

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::reiterator  iter;
       for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
       Cout<<iter->first<<”   ”<<iter->second<<end;
}
}
(3) 數組

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::reiterator  iter;
      int nSize = mapStudent.size();
     for (int nIndex = 1; nIndex <= nSize; nIndex++)
 // for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
    {
       //Cout<<iter->first<<”   ”<<iter->second<<end;
      cout << mapStudent[nIndex] << endl;
 }
}
5、數據查找 find

我們通過find函數 來定位數據出現的位置

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::reiterator  iter;
      iter = mapStudent.find(1);
     if(iter! = mapStudent.end())
    {
        cout << "Find the value is" << iter->second <<endl;
     }
     else
    {
        cout << "do not find"  <<endl;

6、數據的清空clear()與判空empty(),返回ture 這爲空map

7、數據的刪除 erase()

#include <map>
#include <string>
#include <iostream>
Using namespace std;
Int main()
{
       map<int, string> mapStudent;
       mapStudent.insert(pair<int, string>(1, “student_one”));
       mapStudent.insert(pair<int, string>(2, “student_two”));
       mapStudent.insert(pair<int, string>(3, “student_three”));
       map<int, string>::reiterator  iter;
      iter = mapStudent.find(1);
      mapStudent.erase(iter);          // (1)迭代器刪除
     int n = mapStudent.erase(1);  // (2)關鍵字刪除
    //(3)用迭代器,成片的刪除
     mapStudent.erase(mapStudent.begin(),mapStudent.end());
 

for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++)
{
       Cout<<iter->first<<”   ”<<iter->second<<end;
}

8、swap key_comp,value_comp,get_allocator

9、排序(特殊的情況,關鍵字爲一個結構體,排序問題)

#include <map>
#include <string>
Using namespace std;
Typedef struct tagStudentInfo
{
       Int      nID;
       String   strName;
}StudentInfo, *PStudentInfo;  //學生信息
 
Int main()
{
    int nSize;
       //用學生信息映射分數
       map<StudentInfo, int>mapStudent;
    map<StudentInfo, int>::iterator iter;
       StudentInfo studentInfo;
       studentInfo.nID = 1;
       studentInfo.strName = “student_one”;
       mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
       studentInfo.nID = 2;
       studentInfo.strName = “student_two”;
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++)
    cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl;
}
以上程序是無法編譯通過的,只要重載小於號,就OK了,如下:
Typedef struct tagStudentInfo
{
       Int      nID;
       String   strName;
       Bool operator < (tagStudentInfo const& _A) const
       {
              //這個函數指定排序策略,按nID排序,如果nID相等的話,按strName排序
              If(nID < _A.nID)  return true;
              If(nID == _A.nID) return strName.compare(_A.strName) < 0;
              Return false;
       }
}StudentInfo, *PStudentInfo;  //學生信息
第二種:仿函數的應用,這個時候結構體中沒有直接的小於號重載,程序說明
#include <map>
#include <string>
Using namespace std;
Typedef struct tagStudentInfo
{
       Int      nID;
       String   strName;
}StudentInfo, *PStudentInfo;  //學生信息
 
Classs sort
{
       Public:
       Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const
       {
              If(_A.nID < _B.nID) return true;
              If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0;
              Return false;
       }
};
 
Int main()
{
       //用學生信息映射分數
       Map<StudentInfo, int, sort>mapStudent;
       StudentInfo studentInfo;
       studentInfo.nID = 1;
       studentInfo.strName = “student_one”;
       mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90));
       studentInfo.nID = 2;
       studentInfo.strName = “student_two”;
mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80));

10、結尾
map的每個數據對應紅黑樹上的一個節點,這個節點在不保存你的數據時,是佔用16個字節的,一個父節點指針,左右孩子指針,還有一個枚舉值(標示紅黑的,相當於平衡二叉樹中的平衡因子),這些比較佔內存。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章