2018-03-05
Boolan STL 第四周
万用的hashfunction:
使用以Hash Table为底层的容器,比如unordered_map(hash_map),在使用个过程中,需要有计算hash code的方法来计算出元素在hashtable中的具体位置。 那么对于自定义类型来说,需要指定对应的Hash Function,只有这样才能在使用的过程,系统找到对应元素应该插入的位置,以便系统自动管理我们需要插入到容器中的各个元素。
#includeclass Customer{ //........ };
class CustomerHash {
public:
std::size_t operator()(const Customer& c) const
{ return /*........*/; } };
size_t customer_hash_func(const Customer& c)
{ return /*......*/; } //使用 unorder_set customers(20, customer_hash_func);
//以struct hash 偏特化形式实现hash function
class MyString
{
private: char* _data;
size_t _len; };
namespace std;
{
template<>
struct hash
{
size_t operator()(const MyString& s) const noexcept
{ return hash()(string(s.get())); }
}
}
c++ 2.0提供的hash function
//使用万能的Hash Function
class CustomerHash
{
public: std::size_t operator()(const Cunstomer& c) const
{ return hash_val(c.fname, c,Iname, c.no); //在此,只需要调用hash_val函数,并把每个成员的值传递给他就行了
}
}
tuple:任意类型元素组合


Type Traits定义:

Type Traits测试:







TypeTraits 实现:




moveable :可移动的,可以将原先的指针打断,新建的元素指针直接指向元素,提高效率,但必须包拯原来的指针不会再被使用。