VC Hash List

Source: Internet
Author: User
Tags bulk insert traits

VC has 2 versions of the hash list class, Hash_map and Unordered_map,hash_map are located in the Stdext namespace, unordered_map in the Std namespace (vs2008 and subsequent versions are available), The official recommendation is to use Unordered_map, the former is an old non-standard version.

2 are similar in use, but the constructors are significantly different. In cases where a value type such as int is used as key, the last few parameters can use the default value without providing hash function and compare function. But it's a lot more cumbersome to use a special type as a hash key, such as using string strings as hash key values.

template<
ClassKey,
classTy,
classHash = Std::hash<key>
classPred = Std::equal_to<key>
classAlloc = std::allocator<std::p air<ConstKey, ty> > >
classUnordered_map;

Template <
classKey,
classType,
classTraits=hash_compare<key, less<key>;
classAllocator=allocator<pair <ConstKey, type> > >
classHash_map;

There are several concepts in the hash list class that can be understood to be good for understanding these 2 constructors. Key key value, value real value, hash value is the most basic concept of the hash table, when storing value values, the hash table needs to convert the key value to a hash value (the hash value will be used as an array subscript), its type is unsigned shaping. The mapping function of key value--hash value is the hash function, and VC provides default hash functions for some types of keys, such as int and string. Mapping function does not guarantee that the key and hash value mapping relationship is one by one corresponding, there may be multiple key values mapped to a hash value case. At this point we need to compare functions to resolve conflicts, to distinguish between different key values under the same hash value.

Back to the above 2 constructors, Unordered_map is relatively easy to understand, the third entry "class Hash = std::hash<key>", the type required is actually a function object, used to complete the hash map. A function object named hash is used by default. The fourth parameter "class Pred = Std::equal_to<key>", the required type is also a function object, used to complete the hash value after the collision of the Key value of the comparison. For these 2 parameters we can also use a custom function object, such as the following (assuming the type of key is int). In addition, the string type is special and is later said.

structHashmy
{
size_toperator()(int_val)Const{return_val% -;}
};

struct  equalmy
{     //  functor for operator==
    bool  operator () (int  _left, const  int  _right)  const
    {     //  apply operator==  to operands
         return   (_left == _right);
    }
};

Std::unordered_map<int,int,hashmy,equalmy> S3;

The constructor of the Hash_map class is composed of the third entry to complete the hash function and 2 functions of the comparison function. Its default function object is defined as follows

template<classKey,classTraits = less<key> >
classHash_compare
{
Traits comp;
Public:
Constsize_t bucket_size =4;
Constsize_t min_buckets =8;
Hash_compare ();
Hash_compare (Traits pred);
size_toperator( )(Constkey& _key)Const;
BOOLoperator( )(
Constkey& _key1,
Constkey& _key2
)Const;
};

Note that this class does not use Equal_to to make a comparison of key values after the hash value conflict, but instead uses the less function by default. It is said that this is to improve the effectiveness of post-conflict search, when the need to determine the time to pass! (A < b) &&! (b < a) to achieve ..... The custom method is as follows, assuming that key is of type int.

classHash_compare_my
{
Public:
enum
{
Bucket_size =4,
Min_buckets =8
};

size_toperator( )(Constint& _key)Const{return_key% -;}
BOOLoperator( )(Constint& _key1,Constint& _key2)Const
{
return(_key1 < _key2);
}
};

Stdext::hash_map<int,int,hash_compare_my> T;

It is often necessary to use string as key, but neither UNORDERED_MAP nor Hash_map supports the direct use of string because the library does not provide a default comparison function for the string's key. Fortunately, the library provides a hash function of string, and the comparison function is not difficult to implement, although it needs to be customized.

structequal_s
{
BOOLoperator()(ConstSTD::string& S1,ConstSTD::string& S2)Const
{
returnS1.compare (s2) = =0;
}
};
structless_s
{
BOOLoperator()(ConstSTD::string& Str1,ConstSTD::string& str2)Const
{
returnStr1.compare (STR2) <0;
}
};
STD::UNORDERED_MAP&LT;STD::string,int, STD::HASH&LT;STD::string>,equal_s> s;
STDEXT::HASH_MAP&LT;STD::string,int, STDEXT::HASH_COMPARE&LT;STD::string,less_s> > S1;

Finally, the Insert function of the hash list has multiple overloaded versions, and the insert operation fails if it encounters an inserted key already exists, noting that the overloaded version of BULK Insert does not explicitly prompt for failure.

template<classInputiterator>
voidInsert
Inputiterator _first,
Inputiterator _last
);

Pair <iterator,BOOL> Insert (
Constvalue_type& _val
);


VC Hash List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.