1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#ifndef KEYPAIR_H
#define KEYPAIR_H
#include <iostream>
#include <map>
#include <cstdlib>
#include <string>
using namespace std;
template <typename keyType,typename itemType>
class keyPairMap
{
public:
keyPairMap();
void insertItemAndKey(keyType key,itemType item);
void removeItemAndKey(keyType key);
void displayItemAndKey();
private:
std::multimap<keyType,itemType> itemKeyPairMap;
std::multimap<keyType,itemType>::iterator mapIterator;
};
#endif
template <typename keyType,typename itemType>
keyPairMap<keyType,itemType>::keyPairMap()
{
}
template <typename keyType,typename itemType>
void keyPairMap<keyType,itemType>::insertItemAndKey(keyType key,itemType item)
{
int numItemsOfUserEntered=itemKeyPairMap.count(key);
if(numItemsOfUserEntered>0)
{
cout<<"Key already used! Please try again!"<<endl;
}
else
{
itemKeyPairMap.insert(mapIterator,pair<itemType,keyType>(item,key));
}
}
template <typename keyType,typename itemType>
void keyPairMap<keyType,itemType>::removeItemAndKey(keyType key)
{
mapIterator=itemKeyPairMap.find(key);
itemKeyPairMap.erase(mapIterator);
}
template <typename keyType,typename itemType>
void keyPairMap<keyType,itemType>::displayItemAndKey()
{
cout<<"List:"<<endl;
for(multimap<itemType,keyType>::iterator myMapIterator =itemKeyPairMap.begin(); myMapIterator!=itemKeyPairMap.end();++myMapIterator)
{
cout <<"Item:"<<(*myMapIterator).first<<", Key:"<<(*myMapIterator).second << endl;
}
}
|