|
-
November 24th, 2009, 02:14 AM
#1
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??
-
November 24th, 2009, 02:46 AM
#2
Re: C++ STL sample code by Rodney Myers does not compile
You probably have those defined already.
-
November 24th, 2009, 03:18 AM
#3
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
};
}
More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf
Premature optimization is the root of all evil --Donald E. Knuth
Please read Information on posting before posting, especially the info on using [code] tags.
-
November 24th, 2009, 03:29 AM
#4
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
};
}
Last edited by laserlight; November 24th, 2009 at 03:37 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|