C++,
pasted
on Jun 19:
|
#include <map>
#include <iostream>
#include <string>
using namespace std;
template<typename Key, typename T>
multimap<T, Key> swap(map<Key, T>& sourceMap) {
typedef map<Key, T> sourceType;
typedef typename sourceType::const_iterator sourceIterator;
const sourceIterator end = sourceMap.end();
multimap<T, Key> newMap;
for (sourceIterator i = sourceMap.begin(); i != end; i++) {
newMap.insert(make_pair(i->second, i->first));
}
return newMap;
}
int main() {
map <string, unsigned int> testMap;
testMap["aEnumeration1"] = 0;
testMap["aEnumeration2"] = 1;
testMap["aEnumeration3"] = 2;
multimap <unsigned int, string> swappedMap = swap(testMap);
for (unsigned int i = 0; i < swappedMap.size(); i++) {
cout<< "The current (key) element of a multimap <unsigned int, string> is: " << swappedMap.find(i)->first <<endl;
cout<< "The current (mapped) element of a multimap <unsigned int, string> is: " << swappedMap.find(i)->second <<endl;
}
}
|
Output:
|
The current (key) element of a multimap <unsigned int, string> is: 0
The current (mapped) element of a multimap <unsigned int, string> is: aEnumeration1
The current (key) element of a multimap <unsigned int, string> is: 1
The current (mapped) element of a multimap <unsigned int, string> is: aEnumeration2
The current (key) element of a multimap <unsigned int, string> is: 2
The current (mapped) element of a multimap <unsigned int, string> is: aEnumeration3
|
|