There's not a common base class for map and hash_map since hash_map isn't even a standard container and they aren't commonly implemented in remotely the same way. set and map might have a common base class, but that's an implementation detail that you shouldn't rely on because the standard doesn't require it. A better way to design your map wrapper would be with policies:
#include <iostream>
#include <map>
#include <set>
template <typename MapType>
class MapWrap {
MapType m;
public:
typedef MapType value_type;
MapType& get() { return m; }
};
int main()
{
MapWrap<std::map<int, int> > wrap1;
MapWrap<std::set<int> > wrap2;
for (int i = 0; i < 10; i++)
wrap1.get()[i] = i * i;
for (int i = 0; i < 10; i++)
wrap2.get().insert(i);
MapWrap<std::map<int, int> >::value_type::iterator it1 = wrap1.get().begin();
while (it1 != wrap1.get().end()) {
std::cout << it1->first << ' ' << it1->second << '\n';
++it1;
}
std::cout << '\n';
MapWrap<std::set<int> >::value_type::iterator it2 = wrap2.get().begin();
while (it2 != wrap2.get().end()) {
std::cout << *it2 << '\n';
++it2;
}
} You could also inherit from MapType:
#include <iostream>
#include <map>
#include <set>
template <typename MapType>
class MapWrap: public MapType {
};
int main()
{
MapWrap<std::map<int, int> > wrap1;
MapWrap<std::set<int> > wrap2;
for (int i = 0; i < 10; i++)
wrap1[i] = i * i;
for (int i = 0; i < 10; i++)
wrap2.insert(i);
MapWrap<std::map<int, int> >::iterator it1 = wrap1.begin();
while (it1 != wrap1.end()) {
std::cout << it1->first << ' ' << it1->second << '\n';
++it1;
}
std::cout << '\n';
MapWrap<std::set<int> >::iterator it2 = wrap2.begin();
while (it2 != wrap2.end()) {
std::cout << *it2 << '\n';
++it2;
}
} This is slightly more convenient and considerably more dangerous because you have to be careful not to treat MapWrap as a polymorphic type because the standard containers were not designed to be used as base classes.
>> Is there a way I can test/prove that a hash_map is not sorted?
If a hash_map happens to be sorted, that's an implementation detail that you shouldn't rely on. The most efficient implementation for a hash_map does not sort the data, though conscientious implementors may add that feature for your convenience. You would be better off assuming that it isn't sorted than trying to prove that it is an having non-portable code that relies on that knowledge.
Though you can prove that it's sorted if you know about the implementation. :) Check your headers and see what you can find, then you can do some sneaky stuff to get access to the internal table and walk through it from beginning to end to see if every element in sequence is less than the next element.