|
-
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??
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
|