包含頭文件
#include <iostream>
#include <string>
#include <map>ios
1 數據的插入
std::map<int, std::string> mapStudent;
//插入數組,數組的下標實際上就是索引
mapStudent[4] = "fengyuzaitu@126.com";數組
2 數據的遍歷
std::map<int, std::string>::iterator iter;
iter = mapStudent.find(5);
//訪問不到數據的判斷
if (iter == mapStudent.end()) return;less
3 數據的刪除
std::cout << iter->second << std::endl;
//刪除該記錄
mapStudent.erase(iter);ide
注意:
對於容器而言,是否已經遍歷完容器數據,是根據iter是否已經迭代到end()
函數
4 map中數字的排序策略說明指針
爲了實現快速查找,map內部自己就是按序存儲的(例如紅黑樹),在經過鍵值對實現查詢的時候,就會按照key的大小順序排序。map默認是從小到大順序排序,由於map提供了默認的最小比較器排序
template < class Key, class T, class Compare = less<Key>,索引
class Allocator = allocator<pair<const Key,T> > > class map;string
所以若是是須要指定從大到小排序,與less相對的是greater:it
template <class T> struct greater : binary_function <T,T,bool>
{ bool operator() (const T& x, const T& y) const {return x>y;}};