#pragma once #include #include #include #include #include using namespace std; template class MultiMap { private: map > mapOfLists; public: MultiMap(); ~MultiMap(); void Put(const K& key_, const V& value_); void Put(const K& key_, list& listPassedByClient_); void DisplayAllKeys() const; void DisplayAllValues() const; void DisplayAllValuesForAKey(const K& key_) const; void RemoveAllValues(const K& key_); void RemoveAValueForAKey(const K& key_, const V& value_); void RemoveKeyAndValues(const K& key_); pair MakePair(const K& key_, const V& value_); void Put(const pair& pair_); void DisplayMap() const; }; template MultiMap::MultiMap() { } template MultiMap::~MultiMap() { } template void MultiMap::Put(const K& key_, const V& value_) { mapOfLists[key_].push_back(value_); // If it's empty it will add a new one, if not it will push it back on the list } template void MultiMap::Put(const K& key_, list& listPassedByClient_) { typename map >::iterator iter; iter = mapOfLists.find(key_); if (iter == mapOfLists.end()) { mapOfLists[key_] = listPassedByClient_; } else { typename list::iterator iter2; for (iter2 = listPassedByClient_.begin(); iter2 != listPassedByClient_.end(); iter2++) { mapOfLists[key_].push_back(*iter2); } } } template void MultiMap::DisplayAllKeys() const { typename map >::const_iterator iter; for (iter = mapOfLists.begin(); iter != mapOfLists.end(); iter++) { cout << iter->first < void MultiMap::DisplayAllValues() const { typename map >::const_iterator iter; for (iter = mapOfLists.begin(); iter != mapOfLists.end(); iter++) { typename list::const_iterator iter2; for (iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++) { cout << *iter2 < void MultiMap::DisplayMap() const { typename map >::const_iterator iter; for (iter = mapOfLists.begin(); iter != mapOfLists.end(); iter++) { cout << iter->first <::const_iterator iter2; for (iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++) { cout << *iter2 < void MultiMap::DisplayAllValuesForAKey(const K& key_) const { typename map >::const_iterator iter; iter = mapOfLists.find(key_); if (iter == mapOfLists.end()) { cout << "Key doesn't exist" <::const_iterator iter2; for (iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++) { cout << *iter2 < void MultiMap::RemoveAllValues(const K& key_) { typename map >::iterator iter; iter = mapOfLists.find(key_); if (iter == mapOfLists.end()) { cout << "Key doesn't exist" < void MultiMap::RemoveAValueForAKey(const K& key_, const V& value_) { typename map >::iterator iter; iter = mapOfLists.find(key_); if (iter == mapOfLists.end()) { cout << "Key doesn't exist" <::iterator iter2; for (iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++) { if (*iter2 == value_) { valueFound = true; cout << "Removing value " << value_ << " for key: " << key_ <second.erase(iter2); break; } } if (!valueFound) { cout << "Value not found in key: " << key_ << endl; } } } template void MultiMap::RemoveKeyAndValues(const K& key_) { typename map >::iterator iter; iter = mapOfLists.find(key_); if (iter == mapOfLists.end()) { cout << "Key not found..." < pair MultiMap::MakePair(const K& key_, const V& value_) { pair p = make_pair(key_, value_); return p; } template void MultiMap::Put(const pair& pair_) { mapOfLists[pair_.first].push_back(pair_.second); }