Skip to content

Commit b9a179d

Browse files
authored
Create hash_map.cpp
1 parent fcc5595 commit b9a179d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
#include <ext/hash_map>
3+
#include <cstring>
4+
5+
using namespace __gnu_cxx;
6+
7+
using namespace std;
8+
9+
struct eqstr
10+
{
11+
bool operator() (const char* s1, const char* s2) const
12+
{
13+
return strcmp(s1, s2) == 0;
14+
}
15+
};
16+
17+
18+
int main()
19+
{
20+
hash_map<const char*, int, hash<const char*>, eqstr> s;
21+
22+
cout << s.size() << endl;
23+
24+
s["why"] = 3;
25+
s["always"] = 4;
26+
s["me"] = 8;
27+
s["star"] = 9;
28+
s["steve"] = 18;
29+
30+
cout << s.size() << endl;
31+
32+
cout << "why -> " << s["why"] << endl;
33+
cout << "me -> " << s["me"] << endl;
34+
cout << "star -> " << s["star"] << endl;
35+
36+
hash_map<const char*, int, hash<const char*>, eqstr>::iterator it = s.begin();
37+
for (; it != s.end(); it++) {
38+
cout << it->first << " " << it->second << endl;
39+
}
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)