Skip to content

Instantly share code, notes, and snippets.

@superwills
Last active December 16, 2015 15:39
Show Gist options
  • Save superwills/5457289 to your computer and use it in GitHub Desktop.
Save superwills/5457289 to your computer and use it in GitHub Desktop.
Reverses a mapping of map: T -> vector<S> to map: S -> vector<T>
// Reverses a mapping of map: T -> vector<S> to map: S -> vector<T>
template<typename T, typename S>
void reverseMapping( map< T, vector<S> >& oMapping, map< S, vector<T> >& revMapping )
{
for( typename map< T, vector<S> >::iterator iter = oMapping.begin() ; iter != oMapping.end() ; ++iter )
{
for( int i = 0 ; i < iter->second.size() ; i++ )
{
pair< typename map< S, vector<T> >::iterator, bool > res =
revMapping.insert( make_pair( iter->second[i], vector<T>() ) ) ;
res.first->second.push_back( iter->first ) ;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment