C++ STL sample code by Rodney Myers does not compile
The code does not compile as-is.
Original link:
http://frank.mtsu.edu/~csjudy/STL/HashMap.h
Here is the bare-bone code:
#include <vector>
#include <map>
/*
HashMap class template; A usage example for vector, map, and function objects.
Written by Rodney Myers, 3/4/00
*/
namespace Containers
{
template <class Key, class Value, class HashFunc, class Pred=std::less<Key> >
class HashMap: private std::vector<std::map<Key, Value, Pred> >
{
public:
explicit HashMap(int hashFunctionUpperBound)
{
hashFunc = HashFunc();
resize(hashFunctionUpperBound+1);
}
Value& operator[](Key& key)
{
return at(hashFunc(key))[key];
}
bool Find(const Key &key, Value &val) const
{
std::map<Key, Value, Pred>::const_iterator itr;
int index = hashFunc(key);
// search for the key value
if ((itr = at(index).find(key)) == at(index).end())
return false; // key not found
// the key was found, copy the associated value
val = itr->second;
return true;
}
HashFunc hashFunc; // used to store an instance of the hash function object
};
}
However, if I replace the "Key" to be string and "Value" to be "int", it compiles fine.
I don't have a clue as to why this is the case though. Any one has a clue??
Re: C++ STL sample code by Rodney Myers does not compile
You probably have those defined already.
Re: C++ STL sample code by Rodney Myers does not compile
The code is indeed erroneous. You need to qualify superclass functions and a typename is missing:
Code:
#include <vector>
#include <map>
/*
HashMap class template; A usage example for vector, map, and function objects.
Written by Rodney Myers, 3/4/00
*/
namespace Containers
{
template <class Key, class Value, class HashFunc, class Pred=std::less<Key> >
class HashMap: private std::vector<std::map<Key, Value, Pred> >
{
public:
explicit HashMap(int hashFunctionUpperBound)
{
hashFunc = HashFunc();
std::vector<std::map<Key, Value, Pred> >::resize(hashFunctionUpperBound+1);
}
Value& operator[](Key& key)
{
return std::vector<std::map<Key, Value, Pred> >::at(hashFunc(key))[key];
}
bool Find(const Key &key, Value &val) const
{
typename std::map<Key, Value, Pred>::const_iterator itr;
int index = hashFunc(key);
// search for the key value
if ((itr = std::vector<std::map<Key, Value, Pred> >::at(index).find(key)) == std::vector<std::map<Key, Value, Pred> >::at(index).end())
return false; // key not found
// the key was found, copy the associated value
val = itr->second;
return true;
}
HashFunc hashFunc; // used to store an instance of the hash function object
};
}
Re: C++ STL sample code by Rodney Myers does not compile
If an update is indeed made, I would suggest that there be a (private) typedef, e.g.,
Code:
typedef std::vector<std::map<Key, Value, Pred> > parent;
so that the implementation can use parent::at(index) instead of the terribly verbose std::vector<std::map<Key, Value, Pred> >::at(index).
EDIT:
Oh yeah, but now I remember a simpler way to fix this. One can use this-> instead of a typedef:
Code:
#include <vector>
#include <map>
/*
HashMap class template; A usage example for vector, map, and function objects.
Written by Rodney Myers, 3/4/00
*/
namespace Containers
{
template <class Key, class Value, class HashFunc, class Pred=std::less<Key> >
class HashMap: private std::vector<std::map<Key, Value, Pred> >
{
public:
explicit HashMap(int hashFunctionUpperBound)
{
hashFunc = HashFunc();
this->resize(hashFunctionUpperBound+1);
}
Value& operator[](Key& key)
{
return this->at(hashFunc(key))[key];
}
bool Find(const Key &key, Value &val) const
{
typename std::map<Key, Value, Pred>::const_iterator itr;
int index = hashFunc(key);
// search for the key value
if ((itr = this->at(index).find(key)) == this->at(index).end())
return false; // key not found
// the key was found, copy the associated value
val = itr->second;
return true;
}
HashFunc hashFunc; // used to store an instance of the hash function object
};
}