[ create a new paste ] login | about

Link: http://codepad.org/5dnYaqe2    [ raw code | output | fork ]

C++, pasted on Jun 19:
#include <map>
#include <iostream>
#include <string>

using namespace std;

template<typename Key, typename T, typename SourceCompare, typename SourceAllocator>
multimap<Key, T, SourceCompare, SourceAllocator> 
swap(map<Key, T, SourceCompare, SourceAllocator>& sourceMap) {
     typedef map<Key, T, SourceCompare, SourceAllocator> sourceType;
     typedef typename sourceType::const_iterator sourceIterator;
     const sourceIterator end = sourceMap.end();
     multimap<Key, T, SourceCompare, SourceAllocator> 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;
  map <unsigned int, string> swappedMap = swap(testMap);
  cout<< swappedMap[0] <<endl;
  cout<< swappedMap[1] <<endl;
  cout<< swappedMap[2] <<endl;

}


Output:
1
2
3
In function 'int main()':
Line 26: error: conversion from '__gnu_debug_def::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, unsigned int> > >' to non-scalar type '__gnu_debug_def::map<unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > >' requested
compilation terminated due to -Wfatal-errors.


Create a new paste based on this one


Comments: