标签:des style blog class code c
template <class Key, class T, class HashFcn = hash<Key>,
class EqualKey = equal_to<Key>,
class Alloc = alloc>
class hash_map
{
private:
typedef hashtable<pair<const Key, T>, Key, HashFcn,
select1st<pair<const Key, T> >, EqualKey, Alloc> ht;
ht rep; // 底层机制——hash table
....
}
template <class Value, class Key, class HashFcn,
class ExtractKey, class EqualKey,
class Alloc>
class hashtable { // hash table数据结构
public:
typedef Key key_type;
typedef Value value_type;
....
typedef __hashtable_node<Value> node;
typedef simple_alloc<node, Alloc> node_allocator; // 空间配置器
vector<node*,Alloc> buckets; // 桶的集合
....
}
#include <iostream>
#include <hash_map>
#include <cstring>
using namespace std;
using namespace __gnu_cxx;
struct eqstr {
bool operator() (const char *s1, const char *s2) const
{ return strcmp(s1, s2) == 0; }
};
int main()
{
hash_map<const char*, int, hash<const char *>, eqstr> m;
// 两种插入方法
m["hello"] = 123;
m["byebye"] = 234;
m["test"] = 345;
m.insert(pair<const char *, int>("a", 222));
hash_map<const char*, int, hash<const char *>, eqstr>::iterator iter = m.begin();
for ( ; iter != m.end(); ++iter)
cout << iter->first << ‘ ‘ << iter->second << endl;
return 0;
}
template <class T>
struct equal_to : public binary_function<T, T, bool> {
bool operator()(const T& x, const T& y) const { return x == y; }
};
关联容器 — hash_map,布布扣,bubuko.com
标签:des style blog class code c
原文地址:http://blog.csdn.net/nestler/article/details/25597793