-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdict.cc
40 lines (32 loc) · 816 Bytes
/
dict.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <map>
#include <string>
#include "time_counter.h"
template<typename K, typename V>
struct Dictionary {
bool Add(const K& key, const V& value) {
auto result = data_.emplace(key, value);
return result.second;
}
V GetValue(const K& key) const {
auto found = data_.find(key);
if (found == data_.end())
return V();
return found->second;
}
std::map<K, V> data_;
};
int main() {
Dictionary<std::string, int> dict;
for (int i = 1; i < 200; ++i) {
std::string key = "QuiteRandomString" + std::to_string(i);
dict.Add(key, i);
}
TimeCounter counter;
int sum = 0;
for (unsigned i = 0; i < 1000000; ++i) {
sum += dict.GetValue("QuiteRandomString9");
}
std::cout << counter.Elapsed().count()
<< " ms" << std::endl;
}