I came up with the following code which demonstrates a technique for iterating generically over an STL collection and obtaining the key value regardless of how the key is stored.
The context of this is that I am refactoring two functions which both operate the same functionality on two collections: one is a set<int> and the other is a map<int, int> so in the first case I want to act on *it and in the second on it->first (where it is a const_iterator.)
Importantly, I want to do this as the collections are quite large and I don’t want to have to simply create a set from the map just so I can deal with only one specific type.
#include <map>
#include <set>
#include <iostream>
using namespace std;
// General case for obtaining from, say, a set.
template< typename T >
const typename T::key_type getKey( const typename T::const_iterator& it )
{
return *it;
}
// Specific case for a map<int,int>
template<>
const map<int, int>::key_type getKey< map<int, int> >( const map<int, int>::const_iterator& it )
{
return it->first;
}
template< typename T >
void dumpOut( T& coll )
{
for ( typename T::const_iterator it = coll.begin(); it != coll.end(); ++it )
{
const typename T::key_type& a = getKey<T>(it);
cout << a << endl;
}
}
int main()
{
set<int> s1;
s1.insert(10);
s1.insert(15);
s1.insert(20);
dumpOut< set<int> >( s1 );
map<int, int> m1;
m1.insert( pair<int, int>(11, -1) );
m1.insert( pair<int, int>(16, -1) );
m1.insert( pair<int, int>(21, -1) );
dumpOut< map<int, int> >( m1 );
return 0;
}
My question is: Is it possible to make the specialised case for map<int,int> a little more general since the approach would clearly work for map generally, regardless of what the key and value actually are.
Any pointer (no pun intended) would be useful. Please note that I can’t use C++11 solutions although I’m interested in solutions using it from an academic perspective. Thanks.
You have one C++ language problem here – partial specialization of functions is not allowed.
So it cannot be as simple as this:
Fortunately partial specialization of classes is allowed – so change to this way:
I copied your changed example there: http://ideone.com/tE2aC