我收到错误
" hashmap.hpp:63:14:错误:命名空间'std'中的'hash'没有命名 模板类型std :: hash hash;"
当我传入T类时,我不明白为什么我需要实现自己的专业化,这可能是适用于散列函数的类型之一。
hashmap.hpp
#ifndef __HASHMAP_HPP__
#define __HASHMAP_HPP__
#define INITIAL_CAPACITY 10
#include <iostream>
#include <functional>
// A simple node struct for your separate chaining
// You may change this if you want
template <class K,class V>
struct KVNode
{
K key;
V value;
KVNode<K,V>* next;
KVNode(KVNode<K,V>* theNext = NULL) :
next(theNext)
{
}
};
template <class K, class V>
class HashMap {
public:
// Default constructor: creates an empty hash map
HashMap();
// Constructor setting the default value for our map
HashMap(V value);
// Destructor: deallocates all memory associated
// with the hash map
~HashMap();
// size() returns the number of elements currently
// stored in the hash map.
unsigned int size() const;
// set() inserts the given key value pair into the
// hash map. If the key is already in the map, then
// the value for that key is overwritten.
void set(const K& key, const V& value);
// get() returns the value associated with the key.
// If the key is not in the hash map, the default
// value should be returned.
V get(const K& key) const;
private:
// You are allowed to add any private variables you
// need to implement the functionality of a hash map
// It is also okay to add addional public or private
// methods you need, but you may not change the
// already existing method signatures.
// You may find these private members useful, but you are
// not required to use them.
KVNode<K,V>** map;
// To use this hash map with your own key type,
// you may need to provide a specialization of std::hash.
//std::hash<K> hash; #this was previous declaration but caused error
std::hash<K> hash;
// This is the default value to return if a key is not
// in the hash map
V default_value;
};
template <class K, class V>
HashMap<K,V>::HashMap() : default_value()
{
KVNode<K,V>** map = new KVNode<K,V>*[INITIAL_CAPACITY];
for (int i = 0; i < INITIAL_CAPACITY; ++i)
map[i] = new KVNode<K,V>();
}
template <class K, class V>
HashMap<K,V>::HashMap(V value)
{
//TODO: Implement this method
}
template <class K, class V>
HashMap<K,V>::~HashMap()
{
//TODO: Implement this method
}
template <class K, class V>
unsigned int HashMap<K,V>::size() const
{
//TODO: Implement this method
}
template <class K, class V>
void HashMap<K,V>::set(const K& key, const V& value)
{
//TODO: Implement this method
}
template <class K, class V>
V HashMap<K,V>::get(const K& key) const
{
//TODO: Implement this method
}
#endif // __HASHMAP_HPP__
temp.cpp
#include "hashmap.hpp"
#include <iostream>
#include <string>
#include <functional>
using namespace std;
int main()
{
HashMap<string,int> hm = HashMap<string,int>();
return 0;
}