c++问题求救

来源:百度知道 编辑:UC知道 时间:2024/06/10 14:16:28
msdn中对multimsp的描述请问什么意思:
template <
class Key,
class Type,
class Traits=less<Key>,
class Allocator=allocator<pair <const Key, Type> >
>
class multimap

multimap 支持同一个键多次出现的map类型,一般的map是键(key)和值(value)对,就是一个key对应一个value,但是multimap支持一个key对应多个value数组.
怎么存储的呢?这就由后面两个参数来决定,Traits一般是一个函数对象.函数对象,顾名思义,就是在某种方式上表现得象一个函数的对象.典型地,它是指一个类的实例,这个类定义了应用操作符operator(). Allocator是存储分配对象,负责给你的multimap分配存储.该参数可以不传递.

给你一个sample:
#include <map>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
// 定义multimap,以greater做为排序依据
multimap<int, int, greater<int>> map;

// 在key = 1 下插入10,20,30.
// 在key = 2 下插入40.
map.insert(pair<int, int>(1,10));
map.insert(pair<int, int>(1,20));
map.insert(pair<int, int>(1,30));
map.insert(pair<int, int>(2,40));

multimap<int, int, greater<int>>::iterator iter;
// 结果:
// Key:2, value:40
// Key:1, value:10
// Key:1, value:20
// Key:1, value:30
for(iter = map.begin(); iter != m