您的位置:首页 > 其它

STL的map类型

2015-05-30 09:48 183 查看
关于map的数据类型,可以与set进行比较,我们说set,map都是关联性容器,也即set,map都是根据关键字进行数据存储、读取的,我们说set是关键字和数据相同的关联性容器,而map则是不同的,也即利用数学上的映射关系建立的键值对进行存储和读取。

#include<map>
#include<iostream>
using namespace std; struct myComp

{
	int a, b, c;
	myComp() :a(0), b(0), c(0){};
	myComp(int aa, int bb, int cc) :a(aa), b(bb), c(cc){};
	friend bool operator< (const myComp &la, const myComp &lb);
};

inline bool operator< (const myComp &la, const myComp &lb)
{
	if (la.a != lb.a) return la.a < lb.a;
	if (la.b != lb.b) return la.b < lb.b;
	if (la.c != lb.c) return la.c < lb.c;
	return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
	map<myComp, char> imap;
	imap[myComp(4, 5, 0)] = 'q';
	imap[myComp(2, 8, 9)] = 'o';
	imap[myComp(3, 9, 1)] = 'u';
	imap[myComp(3, 10, 1)] = '9';
	imap[myComp(3, 9, 0)] = '0';
	imap[myComp(11, 11, 11)] = 'g';
	map<myComp, char>::iterator iter = imap.begin();
	for (; iter != imap.end(); ++iter)
	{
		char temp[16] = { 0 };
		cout << (iter->first).a << " " << (iter->first).b << " " << (iter->first).c << " " << iter->second << endl;
	}
	return 0;
}
同样的改map的插入操作也是insert_unique操作不允许插入与之前相同的值。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: