码迷,mamicode.com
首页 > 编程语言 > 详细

c++ map

时间:2015-02-14 23:48:19      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:

#include <map>
#include <iostream>

using namespace std;

class Person
{
	public :
	int age;
	int ID;
	Person(int age, int id)
	{
		this->age = age;
		this->ID = id;
	}

};

struct CmpByKey {  
	bool operator()(const Person * k1, const Person * k2) {  

		return k1->age < k2->age;
   }  
};  





map<Person * , int, CmpByKey> iMap;

int main()
{
	Person  * pPerson;
	for(int i = 10; i > 0; i--)
	{
		pPerson = new Person(i, i);
		iMap.insert(pair<Person *, int>(pPerson, i));
	}

	map<Person *,int, CmpByKey>::iterator iBegin = iMap.begin();
	map<Person *,int, CmpByKey>::iterator iEnd = iMap.end();

	while(iBegin != iEnd)
	{
		cout<<iBegin->first->ID << ": ";
		cout << iBegin->first->age  <<  endl;
		iBegin++;
	}
	
	cout<<"####################"<<endl;
	
	pPerson = new Person(1, 10);

	pair<map<Person * , int, CmpByKey>::iterator, bool> Insert_Pair;


	Insert_Pair = iMap.insert(pair<Person *, int>(pPerson, 10));

	if(Insert_Pair.second == true)
    {
        cout<<"Insert Successfully"<<endl;
    }
    else
    {
        cout<<"Insert Failure"<<endl;
    }

	iBegin = iMap.begin();
	while(iBegin != iEnd)
	{
		cout<<iBegin->first->ID << ": ";
		cout << iBegin->first->age<<endl;
		iBegin++;
	}


}

  

c++ map

标签:

原文地址:http://www.cnblogs.com/kisstherain/p/4292286.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!