STL map 用法

包含头文件
#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()
函数

调用erase函数以后,迭代器会失效,须要从新获取新的迭代器指针,进行操做,不然会访问到无效指针

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

相关文章
相关标签/搜索