Commit 0853575c0b68917f70780309b90fde56f3abe7d6

Authored by Grzegorz Jabłoński
1 parent c102e438

Added hash example with std::hash specialisation

examples09/05-unordered_set/makefile
1   -all: unordered_set
  1 +all: unordered_set unordered_set2
2 2  
3 3 unordered_set: unordered_set.o
4 4 g++ -g -Wall -pedantic $^ -o $@
... ... @@ -6,6 +6,13 @@ unordered_set: unordered_set.o
6 6 unoredered_set.o: unordered_set.cpp employee.h
7 7 g++ -g -c -Wall -pedantic $< -o $@
8 8  
  9 +unordered_set2: unordered_set2.o
  10 + g++ -g -Wall -pedantic $^ -o $@
  11 +
  12 +unoredered_set2.o: unordered_set2.cpp employee.h
  13 + g++ -g -c -Wall -pedantic $< -o $@
  14 +
  15 +
9 16 .PHONY: clean
10 17  
11 18 clean:
... ...
examples09/05-unordered_set/unordered_set2.cpp 0 → 100644
  1 +#include <functional>
  2 +#include <unordered_set>
  3 +using namespace std;
  4 +#include "employee.h"
  5 +
  6 +template<> struct std::hash<Employee>
  7 +{
  8 + std::size_t operator()(const Employee & o) const
  9 + {
  10 + return std::hash <std::string>()(o.FirstName)
  11 + ^ (std::hash <std::string>()(o.LastName) << 1)
  12 + ^ (std::hash <std::string>()(o.ID) << 2);
  13 + }
  14 +};
  15 +
  16 +void EmpsetPrint (const unordered_set <Employee> S, ostream & Out);
  17 +
  18 +int
  19 +main ()
  20 +{
  21 + Employee Ben ("Ben", "Keller", "000-00-0000");
  22 + Employee Bill ("Bill", "McQuain", "111-11-1111");
  23 + Employee Dwight ("Dwight", "Barnette", "888-88-8888");
  24 +
  25 + unordered_set <Employee> S;
  26 + S.insert (Bill);
  27 + S.insert (Dwight);
  28 + S.insert (Ben);
  29 + EmpsetPrint (S, cout);
  30 +}
  31 +
  32 +void
  33 +EmpsetPrint (const unordered_set <Employee> S, ostream & Out)
  34 +{
  35 + unordered_set <Employee>::const_iterator It;
  36 + for (It = S.begin (); It != S.end (); ++It)
  37 + Out << *It << endl;
  38 +}
... ...