**Map**是一個關聯容器,它內部有兩個數據,第一個(**first**)稱為關鍵字(**key**),第二個(**second**)稱為關鍵字的值(**value**),**key**與**value**二者是一一對應的(稱為**pair**),且**key**在**map**中關鍵字是唯一的。**map**內部自建一顆嚴格意義上的平衡二叉樹,對數據有排序功能,因此,**map**內部數據都是有排序的(**less**或**greater**)。
具體關于**map**容器類的介紹可以看這:[https://msdn.microsoft.com/en-us/library/s44w4h2s.aspx](https://msdn.microsoft.com/en-us/library/s44w4h2s.aspx)
下面來看看一些常用的方法:
**1、map::insert()**
~~~
map<string, int> student;
student.insert(pair<string, int>("xiaohong", 80));
student.insert(pair<string, int>("xiaofang", 76));//成功
student.insert(pair<string, int>("xiaoming", 94));
student.insert(pair<string, int>("xiaozhang", 80));//key不同,value可以相同
student.insert(pair<string, int>("xiaofang", 88));//存在為"xiaofang"的key,插入失效
~~~
實際上,**map**的**insert**比較簡單,因此不論你怎么插,其內部的平衡二叉樹都會根據關鍵字**key**自動排序。在上述代碼中可知,**key**是**string**類型的,且是唯一的,插入重復的關鍵字的一對數據將會失效;雖然**key**唯一,但是**value**可以相同。(上面可以看作是學生的考試成績,姓名(**key**)與成績(**value**)一一對應)
**2、map::begin()、map::cbegin()、map::end()、map::cend()、**
**map::crbegin()、map::crend()、map::rbegin()、map::rend()**
這些就不說了。
**3、map::at()、map::iterator**(元素的訪問)
~~~
map<string, int>::iterator it;
for (it = student.begin(); it != student.end(); ++it){
cout<<it->first<<" "<<it->second<<endl;
}
cout<<student.at("xiaoming")<<endl;
cout<<student.at("xiaozhang")<<endl;
~~~
實際上,關鍵字**key**存放在**map**中為**first**,關鍵字的值**value**在**map**中為**second**,使用的是迭代器來訪問。
**at()**訪問時,帶入的參數是關鍵字,返回關鍵字所對應的值,上面的兩個輸出分別是:94、80。
**4、map::count()**
~~~
cout<<student.count("xiaohong")<<endl;//輸出1
cout<<student.count("xiaoHong")<<endl;//輸出0
~~~
這個方法是用來判斷**map**中是否存在某個關鍵字。存在返回**1**,不存在返回**0**。
**5、map::find()**
~~~
map<string, int>::iterator it;
it = student.find("xiaoming");
cout<<it->first<<" "<<it->second<<endl;
~~~
查找,返回所查找關鍵字指向的迭代器。
**6、map::equal_range()**
~~~
pair<map<string, int>::iterator, map<string, int>::iterator> it;
it = student.equal_range("xiaoho");
cout<<it.first->first<<" "<<it.first->second<<endl;
cout<<it.second->first<<" "<<it.second->second<<endl;
~~~
返回一對迭代器。第一個迭代器指向的是**map**中第一個**大于key**的迭代器,第二個迭代器指向的是**map**中第一個**大于或等于key**的迭代器。
**7、map::erase()**
~~~
student.erase("xiaofang");//刪除關鍵字為"xiaofang"的pair
student.erase(student.begin());//刪除起始位置的pair
student.erase(next(student.begin()), prev(student.end()));//保留第一個和最后一個pair,其余的全部刪除
~~~
刪除某個**關鍵字**所關聯的**pair of key-value**或某個**位置**或某個**連續位置**的**pair of key-value**。
**8、map::clear()、map::empty()、map::size()**
~~~
student.clear();
cout<<student.empty()<<endl;
cout<<student.size()<<endl;
~~~
**map::clear()**是刪除**map**中的所有**pair**,**map::empty()**返回一個**bool**類型值,**true**表示空,**flase**表示非空,**map::size()**返回**map**中**pair**的個數。
**9、其他的用法**
~~~
vector<pair<string, int> >s;
s.push_back(make_pair("ads", 3));
s.push_back(make_pair("edf", 6));
s.push_back(make_pair("ifd", 9));
vector<pair<string, int>>::iterator iw;
for(iw = s.begin(); iw != s.end(); ++iw){
cout<<iw->first<<" "<<iw->second<<endl;
}
cout<<endl<<endl;
student.insert(s.begin(), s.end());
for (it = student.begin(); it != student.end(); ++it){
cout<<it->first<<" "<<it->second<<endl;
}
~~~
比如將**vector**轉換成**map**來使用也是可以的。
當然,還有一些其他用法,用到的時候再查,這里就不介紹了。