stl map
来源:互联网 发布:宁海哪里有学淘宝设计 编辑:程序博客网 时间:2024/05/14 23:17
通常在使用STL时,除了List和vector,我们还会经常用到Map。Map使用关键值_Key来唯一标识每一个成员_Tp。STL中的Map声明如下:
template < class _Key, class _Tp, class _Compare = less<_Key>, class _Alloc = allocator<pair<const _Key, _Tp> > >class map{...}其中_Compare是用来对_Tp进行排序用的,以便提高查询效率。缺省是less<_Key>原型如下:
template <class _Tp>struct less : public binary_function<_Tp,_Tp,bool>{ bool operator()(const _Tp& __x, const _Tp& __y) const { return __x < __y; }};它是一个Functor,留给Map排序用。不过平时我们也可以使用它:例:
std::less<int> MyLess;bool bRetVal = MyLess(3, 12);cout << ((bRetVal == true) ? "Less" : "Greater") << endl;OK,下面就来示范一个:
#include<map>#include<iostream> using namespace std; typedef map<int, string, less<int> > M_TYPE;typedef M_TYPE::iterator M_IT;typedef M_TYPE::const_iterator M_CIT; int main(){ M_TYPE MyTestMap; MyTestMap[3] = "No.3"; MyTestMap[5] = "No.5"; MyTestMap[1] = "No.1"; MyTestMap[2] = "No.2"; MyTestMap[4] = "No.4"; M_IT it_stop = MyTestMap.find(2); cout << "MyTestMap[2] = " << it_stop->second << endl; it_stop->second = "No.2 After modification"; cout << "MyTestMap[2] = " << it_stop->second << endl; cout << "Map contents : " << endl; for(M_CIT it = MyTestMap.begin(); it != MyTestMap.end(); it++) { cout << it->second << endl; } return 0;}程序输出:MyTestMap[2] = No.2MyTestMap[2] = No.2 After modificationMap contents :No.1No.2 After modificationNo.3No.4No.5可见Map已经根据less<int>对各个成员_Tp进行从小到大进行排了序;同理,若我们不使用less,改而使用greater<int>,则Map将成员从大到小排序。
- STL-map
- stl -map
- stl map
- STL Map
- STL---map
- STL(MAP)
- STL(map)
- STL--map
- STL map
- stl map
- STL map
- STL----map
- stl map
- STL map
- 【STL】map
- STL--map
- STL map
- STL map
- SGI STL V3.2 源码剖析笔记-3. vector(未完待续)
- static_cast、dynamic_cast、reinterpret_cast、和const_cast
- stdafx.h、stdafx.cpp的作用
- Eclipse WTP 入门
- stdafx.h的作用
- stl map
- STL map常用操作简介
- 最优秀的stl学习网站
- STL之bind2st for_each 和 transform
- STL知识点(常用算法函数介绍 、容器、类属算法、迭代器、函数对象、适配器、分配器
- 胡乱的记一下 vector 的 clear 的功能。。
- vector::clear()和vecotr::pop_back()对内存处理的不同
- 迅雷6
- Iterator Classes (迭代器类)