hash_map如下定义,其接口和stl中的定义不同。 template <
class Key,
class Type,
class Traits=hash_compare<Key, less<Key> >,
class Allocator=allocator<pair <const Key, Type> >
>
class hash_map
sgi stl中那个hash_map是根据hash值进行比较,但是这个hash_map却不需要,它不需要提供hash值,只需要提供一个比较函数,所以有点迷惑。
msdn中有如下例子, ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vcstdlib/html/c11d6729-c571-43a1-96fd-360ec678b23a.htmhas a sample, there are 2 structs:struct greater_str {
bool operator()(const MyStr & x, const MyStr & y) const {
if ( strcmp(x, y) < 0)
return true; return false;
}
};
struct less_str {
bool operator()(const MyStr & x, const MyStr & y) const {
if ( strcmp(x, y) > 0)
return true; return false;
}
};
我现在想知道的是,采用上面两个不同的比较函数定义的以下两个哈希表,他们有何区别:
hash_map <MyStr, MyInt, hash_compare <MyStr, greater_str >>
hash_map <MyStr, MyInt, hash_compare <MyStr, less_str >>
class Key,
class Type,
class Traits=hash_compare<Key, less<Key> >,
class Allocator=allocator<pair <const Key, Type> >
>
class hash_map
sgi stl中那个hash_map是根据hash值进行比较,但是这个hash_map却不需要,它不需要提供hash值,只需要提供一个比较函数,所以有点迷惑。
msdn中有如下例子, ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_vcstdlib/html/c11d6729-c571-43a1-96fd-360ec678b23a.htmhas a sample, there are 2 structs:struct greater_str {
bool operator()(const MyStr & x, const MyStr & y) const {
if ( strcmp(x, y) < 0)
return true; return false;
}
};
struct less_str {
bool operator()(const MyStr & x, const MyStr & y) const {
if ( strcmp(x, y) > 0)
return true; return false;
}
};
我现在想知道的是,采用上面两个不同的比较函数定义的以下两个哈希表,他们有何区别:
hash_map <MyStr, MyInt, hash_compare <MyStr, greater_str >>
hash_map <MyStr, MyInt, hash_compare <MyStr, less_str >>
只需要比较key是否相等就行了,这个干吗还非要排出个大小来?
还有就是数据结构说了只需要比较哈希值,此处是直接比较key,
如此说来这个hash_map效率岂不是很低?