C++ multimap

map/multimap,set/multiset都是关联容器。定义时,set与map不同的地方在于:set仅有key_type类型,它的value_type也就是key_type;set没有下标操作。

#include "stdafx.h"
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;

struct ltstr{   // when the first arg of multimap is a user-definded type,we must provide a method for ordering
	bool operator()(const char*s1,const char *s2) const{
		return strcmp(s1,s2)>0;
	}
};

int main()
{
	multimap<const char*,int,ltstr> m;
	multimap<const char*,int,ltstr>::iterator it;
	
	m.insert(pair<const char* const,int>("a",1));
	m.insert(pair<const char* const,int>("c",2));
	m.insert(pair<const char* const,int>("b",3));
	m.insert(pair<const char* const,int>("b",4));
	m.insert(pair<const char* const,int>("a",5));
	cout<<"num of elem with key a:"<<m.count("a")<<endl;
	cout<<"num of elem with key b:"<<m.count("b")<<endl;
	cout<<"num of elem with key c:"<<m.count("c")<<endl;
	
	cout<<"elem in m:"<<endl;
	for(it=m.begin();it!=m.end();++it)
		cout<<" ["<<(*it).first<<","<<(*it).second<<"]"<<endl;

	
	return 0;
		
}


你可能感兴趣的:(C++)