Kojo K.

asked • 06/23/20

Rehashing requires recomputing the hash function for all items in the hash table. Read more.....

Since computing the hash function is expensive, suppose

objects provide a hash member function of their own, and each object

stores the result in an additional data member the rst time the hash

function is computed for it. Show how such a scheme would apply for the

Employee class in the implementation below , and explain under

what circumstances the remembered hash value remains valid in each

Employee.


The implementation

// Example of an Employee class

class Employee

{

public:

const string & getName( ) const

{ return name; }


bool operator==( const Employee & rhs ) const

{ return getName( ) == rhs.getName( ); }

bool operator!=( const Employee & rhs ) const

{ return !( *this == rhs; }


// Additional public members not shown


private:

string name;

double salary;

int seniority;


// Additional private members not shown

};


template<>

class hash<Employee>

{

public:

size_t operator()( const Employee & item )

{

static hash<string> hf;

return hf( item.getName( ) );

}

};

1 Expert Answer

By:

Patrick B. answered • 06/23/20

Tutor
4.7 (31)

Math and computer tutor/teacher

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.