c++ STL map 結構體

Map是STL的一個關聯容器,它提供一對一(其中第一個能夠稱爲關鍵字,每一個關鍵字只能在map中出現一次,第二個可能稱爲該關鍵字的值)的數據處理能力,因爲這個特性,它完成有可能在咱們處理一對一數據的時候,在編程上提供快速通道。這裏說下map內部數據的組織,map內部自建一顆紅黑樹(一種非嚴格意義上的平衡二叉樹),這顆樹具備對數據自動排序的功能,因此在map內部全部的數據都是有序的,後邊咱們會見識到有序的好處。
ios

在一些特殊狀況,好比關鍵字是一個結構體,涉及到排序就會出現問題,由於它沒有小於號操做,insert等函數在編譯的時候過不去,下面給出兩個方法解決這個問題web

第一種:小於號重載,程序舉例編程

#include <map>數組

#include <string>ide

Using namespace std;函數

Typedef struct tagStudentInfospa

{orm

       Int      nID;排序

       String   strName;three

}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));

}

  1. /****************************************************************** 
  2.       map的基本操做函數: 
  3.       C++ Maps是一種關聯式容器,包含「關鍵字/值」對 
  4.       begin()          返回指向map頭部的迭代器 
  5.       clear()         刪除全部元素 
  6.       count()          返回指定元素出現的次數 
  7.       empty()          若是map爲空則返回true 
  8.       end()            返回指向map末尾的迭代器 
  9.       equal_range()    返回特殊條目的迭代器對 
  10.       erase()          刪除一個元素 
  11.       find()           查找一個元素 
  12.       get_allocator()  返回map的配置器 
  13.       insert()         插入元素 
  14.       key_comp()       返回比較元素key的函數 
  15.       lower_bound()    返回鍵值>=給定元素的第一個位置 
  16.       max_size()       返回能夠容納的最大元素個數 
  17.       rbegin()         返回一個指向map尾部的逆向迭代器 
  18.       rend()           返回一個指向map頭部的逆向迭代器 
  19.       size()           返回map中元素的個數 
  20.       swap()            交換兩個map 
  21.       upper_bound()     返回鍵值>給定元素的第一個位置 
  22.       value_comp()      返回比較元素value的函數
  1. ==================================================================== 
  2. 一、map構造 
  3.     map<int, string> mapStudent; 
  4.  
  5. 二、map添加數據 
  6.     mapStudent.insert(pair<int, string>(1, "student_one")); 
  7.     mapStudent.insert(map<int, string>::value_type(2, "student_two")); 
  8.     mapStudent[3] = "student_three"; 
  9.  
  10. ********************************************************************/ 
  11. #pragma warning (disable:4786) 
  12. #include <map> 
  13. #include <string> 
  14. #include <iostream> 
  15.  
  16. using namespace std; 
  17.  
  18. int main() 
  19.     map<int, string> mapStudent; 
  20.     cout<<"三種插入方式:"<<endl; 
  21.  
  22.     mapStudent.insert(pair<int, string>(1, "student_one")); 
  23.     mapStudent.insert(map<int, string>::value_type(2, "student_two")); 
  24.     mapStudent[3] = "student_three"
  25.     mapStudent.insert(map<int, string>::value_type(4, "student_four"));    
  26.      
  27.     pair<map<int,string>::iterator,bool> InsertPair;   //判斷是否插入成功 
  28.     InsertPair = mapStudent.insert(map<int,string>::value_type(5,"student_five")); 
  29.     if(InsertPair.second == true
  30.     { 
  31.         //cout<<InsertPair.first.operator++<<endl;  //求解??不知道怎麼應用第一個數據 
  32.     } 
  33.  
  34.     cout<<"三種遍歷方式:"<<endl; 
  35.  
  36.     map<int, string>::iterator  iter; 
  37.     for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 
  38.     { 
  39.         cout<<iter->first<<" "<<iter->second<<endl; 
  40.     } 
  41.  
  42.     map<int, string>::reverse_iterator  iters; 
  43.     for(iters = mapStudent.rbegin(); iters != mapStudent.rend(); iters++) 
  44.     { 
  45.         cout<<iters->first<<" "<<iters->second<<endl; 
  46.     }  //逆序輸出 
  47.     cout<<"數組的輸出形式:"<<endl; 
  48.     for(int iIndex=0;iIndex < mapStudent.size();iIndex++) //size()返回成員的個數 
  49.     { 
  50.         cout<<mapStudent[iIndex]<<endl; 
  51.     } 
  52.     cout<<mapStudent.count(1)<<endl;  //count()判斷關鍵字是否存在,返回1表示存在,0 
  53.     iter = mapStudent.find(1);        //find()關鍵字存在時,返回數據所在位置的迭代器,不然返回end()返回的迭代器 
  54.     if( iter != mapStudent.end() ) 
  55.     { 
  56.         cout<<"數據存在:"<<iter->first<<" "<<iter->second<<endl; 
  57.         mapStudent.erase(iter);        //用迭代刪除數據 
  58.     } 
  59.     else 
  60.     { 
  61.         cout<<"數據不存在!"<<endl; 
  62.     } 
  63.     int n = mapStudent.erase(3);        //用關鍵字刪除,若是刪除了會返回1,不然返回0 
  64.  
  65.     iter = mapStudent.lower_bound(2);   //返回2的迭代器 
  66.     cout<<iter->second<<endl; 
  67.     iter = mapStudent.upper_bound(2);   //返回3的迭代器 
  68.     cout<<iter->second<<endl;     
  69.  
  70.     /*Equal_range函數返回一個pair,pair裏面第一個變量是Lower_bound返回的迭代器,pair裏面第二個迭代器是Upper_bound返回的迭代器,若是這兩個迭代器相等的話,則說明map中不出現這個關鍵字*/ 
  71.     pair<map<int,string>::iterator,map<int,string>::iterator> MapPair; 
  72.     MapPair = mapStudent.equal_range(2); 
  73.     if( MapPair.first == MapPair.second ) 
  74.     { 
  75.         cout<<"Do not find"<<endl; 
  76.     } 
  77.     else 
  78.     { 
  79.         cout<<"Find"<<endl; 
  80.     } 
  81.  
  82.     //刪除一個前閉後開的集合,這是STL的特性 
  83.     mapStudent.earse(mapStudent.begin(), mapStudent.end());     
  84. }
相關文章
相關標籤/搜索