To download the code, click here

01: #include <iostream>
02: #include <fstream>
03: #include <string>
04: #include <vector>
05: #include <map>
06: #include <set>
07: 
08: 
09: #ifndef DMCGRATH_EXAMPLE_HPP
10: #define DMCGRATH_EXAMPLE_HPP
11: 
12: class hash{
13: public:
14:         hash(){}
15:         
16:         bool insert(std::string key, int value){
17:                 if (this->table.find(key) == this->table.end()){
18:                         //key not there
19:                         this->table[key] = value;       
20:                         this->keys.insert(key);
21:                         this->values.push_back(value);
22:                 }
23:                 else{
24:                         //key there
25:                         return false;
26:                 }
27:                 return true;
28:         }
29:         
30:         int size(){
31:                 return keys.size();
32:         }
33:         
34:         friend bool operator==(const hash &LHS, const hash &RHS);
35:         friend ostream& operator<<(ostream &outs, const hash &RHS);
36:         friend istream& operator>>(istream &ins, const hash &RHS);
37:         
38: protected:
39:         std::map<std::string, int> table;
40:         std::set<std::string> keys;
41:         std::vector<int> values;
42: };
43: 
44: 
45: bool operator==(const hash &LHS, const hash &RHS){
46:         //== if all same keys, and point to same values
47:         
48:         if (LHS.keys.size() != RHS.keys.size()){
49:                 return false;
50:         }
51:         
52:         std::map<std::string, int>::iterator ri;
53:         std::map<std::string, int>::iterator li;
54:         
55:         bool broken = false;
56:         
57:         for(ri = RHS.table.begin(), li = LHS.table.begin(); 
58:                 ri != RHS.table.end() && lr != LHS.table.end();
59:                 ++li, ++ri){
60:                         if(li->first != ri->first || li->second != ri->second){
61:                                 broken = true;
62:                                 break;
63:                         }
64:         }
65:         return !broken;
66: }
67: 
68: 
69: //allows me to write
70: /*
71: * hash h;
72: * //fill in h
73: * std::cout << h << std::endl;
74: * 
75: */
76: friend ostream& operator<<(ostream &outs, const hash &RHS){
77:         std::map<std::string, int>::iterator ri;
78:         
79:         for(ri = RHS.table.begin(); ri != RHS.table.end(); ++ri){
80:                 outs << ri->first << " : " << ri->second << std::endl;
81:         }
82:         
83:         return outs;
84: }
85: 
86: friend istream& operator>>(istream &ins, const hash &RHS){
87:         std::string key;
88:         int value;
89:         char colon;
90:         
91:         ins >> key >> colon >> value;
92:         RHS.insert(key, value);
93:         
94:         return ins;
95: }
96: 
97: 
98: 
99: #endif