以前寫過一篇關於map容器的一篇博客,可是隻有一些基礎操做,後來我在CSDN上看到了一位大佬寫的博客因而轉載過來了。ios
做者大大的博客https://blog.csdn.net/sunshinewave/article/details/8067862編程
—————————————————————————————————————————————————————————————————————————————————數組
map是STL的一個關聯容器,它提供一對一(其中第一個能夠稱爲關鍵字,每一個關鍵字只能在map中出現一次,第二個可能稱爲該關鍵字的值)的數據處理能力,因爲這個特性,它完成有可能在咱們處理一對一數據的時候,在編程上提供快速通道。這裏說下map內部數據的組織,map內部自建一顆紅黑樹(一種非嚴格意義上的平衡二叉樹),這顆樹具備對數據自動排序的功能,因此在map內部全部的數據都是有序的,後邊咱們會見識到有序的好處。less
map<int, string> mapStudent;
map<int, string> mapStudent;
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent;//pair<int,string>p;p=make_pair(v1,v2); 8 mapStudent.insert(pair<int, string>(1, "student_one")); 9 mapStudent.insert(pair<int, string>(2, "student_two")); 10 mapStudent.insert(pair<int, string>(3, "student_three")); 11 map<int, string>::iterator iter; 12 for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 13 { 14 cout<<iter->first<<" "<<iter->second<<endl; 15 } 16 }
1 make_pair()//返回類型爲對應的pair類型 2 無需寫出類別,就能夠生成一個pair對象 3 例: 4 make_pair(1,'@') 5 而沒必要費力的寫成 6 pair<int ,char>(1,'@')
第二種:用insert函數插入value_type數據,下面舉例說明函數
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent; 8 mapStudent.insert(map<int, string>::value_type (1, "student_one")); 9 mapStudent.insert(map<int, string>::value_type (2, "student_two")); 10 mapStudent.insert(map<int, string>::value_type (3, "student_three")); 11 map<int, string>::iterator iter; 12 for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 13 { 14 cout<<iter->first<<" "<<iter->second<<endl; 15 } 16 }
第三種:用數組方式插入數據,下面舉例說明ui
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent; 8 mapStudent[1] = "student_one"; 9 mapStudent[2] = "student_two"; 10 mapStudent[3] = "student_three"; 11 map<int, string>::iterator iter; 12 for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 13 { 14 cout<<iter->first<<" "<<iter->second<<endl; 15 } 16 }
以上三種用法,雖然均可以實現數據的插入,可是它們是有區別的,固然了第一種和第二種在效果上是完成同樣的,用insert函數插入數據,在數據的插入上涉及到集合的惟一性這個概念,即當map中有這個關鍵字時,insert操做是插入數據不了的,可是用數組方式就不一樣了,它能夠覆蓋之前該關鍵字對應的值,用程序說明spa
1 mapStudent.insert(map<int, string>::value_type (1, "student_one")); 2 mapStudent.insert(map<int, string>::value_type (1, "student_two"));
1 Pair<map<int, string>::iterator, bool> Insert_Pair; 2 Insert_Pair = mapStudent.insert(map<int, string>::value_type (1, "student_one"));
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent; 8 Pair<map<int, string>::iterator, bool> Insert_Pair; 9 Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_one")); 10 If(Insert_Pair.second == true) 11 { 12 cout<<"Insert Successfully"<<endl; 13 } 14 Else 15 { 16 cout<<"Insert Failure"<<endl; 17 } 18 Insert_Pair = mapStudent.insert(pair<int, string>(1, "student_two")); 19 If(Insert_Pair.second == true) 20 { 21 cout<<"Insert Successfully"<<endl; 22 } 23 Else 24 { 25 cout<<"Insert Failure"<<endl; 26 } 27 map<int, string>::iterator iter; 28 for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) 29 { 30 cout<<iter->first<<" "<<iter->second<<endl; 31 } 32 }
你們能夠用以下程序,看下用數組插入在數據覆蓋上的效果.net
#include <map> #include <string> #include <iostream> using namespace std; int main() { map<int, string> mapStudent; mapStudent[1] = "student_one"; mapStudent[1] = "student_two"; mapStudent[2] = "student_three"; map<int, string>::iterator iter; for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) { cout<<iter->first<<" "<<iter->second<<endl; } }
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent; 8 mapStudent.insert(pair<int, string>(1, "student_one")); 9 mapStudent.insert(pair<int, string>(2, "student_two")); 10 mapStudent.insert(pair<int, string>(3, "student_three")); 11 map<int, string>::reverse_iterator iter; 12 for(iter = mapStudent.rbegin(); iter != mapStudent.rend(); iter++) 13 { 14 cout<<iter->first<<" "<<iter->second<<endl; 15 } 16 }
第三種:用數組方式,程序說明以下指針
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent; 8 mapStudent.insert(pair<int, string>(1, "student_one")); 9 mapStudent.insert(pair<int, string>(2, "student_two")); 10 mapStudent.insert(pair<int, string>(3, "student_three")); 11 int nSize = mapStudent.size() 12 //此處有誤,應該是 for(int nIndex = 1; nIndex <= nSize; nIndex++) 13 //by rainfish 14 for(int nIndex = 0; nIndex < nSize; nIndex++) 15 { 16 cout<<mapStudent[nIndex]<<end; 17 } 18 }
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent; 8 mapStudent.insert(pair<int, string>(1, "student_one")); 9 mapStudent.insert(pair<int, string>(2, "student_two")); 10 mapStudent.insert(pair<int, string>(3, "student_three")); 11 map<int, string>::iterator iter; 12 iter = mapStudent.find(1); 13 if(iter != mapStudent.end()) 14 { 15 cout<<"Find, the value is "<<iter->second<<endl; 16 } 17 Else 18 { 19 cout<<"Do not Find"<<endl; 20 } 21 }
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent; 8 mapStudent[1] = "student_one"; 9 mapStudent[3] = "student_three"; 10 mapStudent[5] = "student_five"; 11 map<int, string>::iterator iter; 12 iter = mapStudent.lower_bound(2); 13 { 14 //返回的是下界3的迭代器 15 cout<<iter->second<<endl; 16 } 17 iter = mapStudent.lower_bound(3); 18 { 19 //返回的是下界3的迭代器 20 cout<<iter->second<<endl; 21 } 22 iter = mapStudent.upper_bound(2); 23 { 24 //返回的是上界3的迭代器 25 cout<<iter->second<<endl; 26 } 27 iter = mapStudent.upper_bound(3); 28 { 29 //返回的是上界5的迭代器 30 cout<<iter->second<<endl; 31 } 32 Pair<map<int, string>::iterator, map<int, string>::iterator> mapPair; 33 mapPair = mapStudent.equal_range(2); 34 if(mapPair.first == mapPair.second) 35 { 36 cout<<"Do not Find"<<endl; 37 } 38 Else 39 { 40 cout<<"Find"<<endl; 41 } 42 mapPair = mapStudent.equal_range(3); 43 if(mapPair.first == mapPair.second) 44 { 45 cout<<"Do not Find"<<endl; 46 } 47 Else 48 { 49 cout<<"Find"<<endl; 50 } 51 }
1 #include <map> 2 #include <string> 3 #include <iostream> 4 using namespace std; 5 int main() 6 { 7 map<int, string> mapStudent; 8 mapStudent.insert(pair<int, string>(1, "student_one")); 9 mapStudent.insert(pair<int, string>(2, "student_two")); 10 mapStudent.insert(pair<int, string>(3, "student_three")); 11 //若是你要演示輸出效果,請選擇如下的一種,你看到的效果會比較好 12 //若是要刪除1,用迭代器刪除 13 map<int, string>::iterator iter; 14 iter = mapStudent.find(1); 15 mapStudent.erase(iter); 16 //若是要刪除1,用關鍵字刪除 17 int n = mapStudent.erase(1);//若是刪除了會返回1,不然返回0 18 //用迭代器,成片的刪除 19 //一下代碼把整個map清空 20 mapStudent.earse(mapStudent.begin(), mapStudent.end()); 21 //成片刪除要注意的是,也是STL的特性,刪除區間是一個前閉後開的集合 22 //自個加上遍歷代碼,打印輸出吧 23 }
1 #include <map> 2 #include <string> 3 uing namespace std; 4 Typedef struct tagStudentInfo 5 { 6 int nID; 7 String strName; 8 }StudentInfo, *PStudentInfo; //學生信息 9 int main() 10 { 11 int nSize; 12 //用學生信息映射分數 13 map<StudentInfo, int>mapStudent; 14 map<StudentInfo, int>::iterator iter; 15 StudentInfo studentInfo; 16 studentInfo.nID = 1; 17 studentInfo.strName = "student_one" 18 mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90)); 19 studentInfo.nID = 2; 20 studentInfo.strName = "student_two"; 21 mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80)); 22 for (iter=mapStudent.begin(); iter!=mapStudent.end(); iter++) 23 cout<<iter->first.nID<<endl<<iter->first.strName<<endl<<iter->second<<endl; 24 }
以上程序是沒法編譯經過的,只要重載小於號,就OK了,以下:code
1 Typedef struct tagStudentInfo 2 { 3 int nID; 4 String strName; 5 Bool operator < (tagStudentInfo const& _A) const 6 { 7 //這個函數指定排序策略,按nID排序,若是nID相等的話,按strName排序 8 If(nID < _A.nID) return true; 9 If(nID == _A.nID) return strName.compare(_A.strName) < 0; 10 Return false; 11 } 12 }StudentInfo, *PStudentInfo; //學生信息
第二種:仿函數的應用,這個時候結構體中沒有直接的小於號重載,程序說明
1 #include <map> 2 #include <string> 3 using namespace std; 4 Typedef struct tagStudentInfo 5 { 6 int nID; 7 String strName; 8 }StudentInfo, *PStudentInfo; //學生信息 9 class sort 10 { 11 Public: 12 Bool operator() (StudentInfo const &_A, StudentInfo const &_B) const 13 { 14 If(_A.nID < _B.nID) return true; 15 If(_A.nID == _B.nID) return _A.strName.compare(_B.strName) < 0; 16 Return false; 17 } 18 }; 19 int main() 20 { 21 //用學生信息映射分數 22 map<StudentInfo, int, sort>mapStudent; 23 StudentInfo studentInfo; 24 studentInfo.nID = 1; 25 studentInfo.strName = "student_one"; 26 mapStudent.insert(pair<StudentInfo, int>(studentInfo, 90)); 27 studentInfo.nID = 2; 28 studentInfo.strName = "student_two"; 29 mapStudent.insert(pair<StudentInfo, int>(studentInfo, 80)); 30 }