zoukankan      html  css  js  c++  java
  • c++ map

    #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++;
    	}
    
    
    }
    

      

  • 相关阅读:
    问题集
    第04次作业-树
    06-图
    05-查找
    04-树
    03-栈和队列
    02-线性表
    01-抽象数据类型
    C语言--总结报告
    C语言--函数嵌套
  • 原文地址:https://www.cnblogs.com/kisstherain/p/4292286.html
Copyright © 2011-2022 走看看