C++ Unordered_set Library - hash_function



Description

It returns the hash function object used by the unordered_set container.

Declaration

Following is the declaration for std::unordered_set::hash_function.

C++11

hasher hash_function() const;

Parameters

none

Return value

It returns the hash function.

Exceptions

Exception is thrown if any element comparison object throws exception.

Please note that invalid arguments cause undefined behavior.

Time complexity

constant time.

Example

The following example shows the usage of std::unordered_set::hash_function.

#include <iostream>
#include <string>
#include <unordered_set>

typedef std::unordered_set<std::string> stringset;

int main () {
   stringset myset;

   stringset::hasher fn = myset.hash_function();

   std::cout << "that contains: " << fn ("that") << std::endl;
   std::cout << "than contains: " << fn ("than") << std::endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

that: 15843861542616104093
than: 18313131606624605886
unordered_set.htm
Advertisements