-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.cc
48 lines (38 loc) · 1.47 KB
/
example.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
41
42
43
44
45
46
47
48
#define OK_LIB_DEFINE
#include "ok_map.h"
#include <iostream>
/*
Example using the C++ wrapper for ok_lib's hashmap.
*/
int main() {
std::cout << "Hello from C++!" << std::endl;
//ok::map<std::string, std::string> map; // Disabled for now, failing in older versions of gcc/clang.
ok::map<const char *, const char *> map;
// Put method
map.put("dave", "bananas");
map.put("mary", "grapes");
// Subscript operator
map["sue"] = "beans";
map["john"] = "fries";
// Overwrite existing value
map["john"] = "salad";
// When the mapping exists, subscript[] and get() are the same.
std::cout << "john: " << map["john"] << "." << std::endl;
std::cout << "mary: " << map.get("mary") << "." << std::endl;
// Overwrite again, via the pointer
auto *p = map.get_ptr("john");
*p = "fries";
std::cout << "john: no, wait, " << map.get("john") << "!" << std::endl;
// Examples when key doesn't exist
std::cout << "Map size: " << map.size() << std::endl;
map.get("cyrus"); // Returns NULL
std::cout << "Map size before map[\"cyrus\"]: " << map.size() << std::endl;
map["cyrus"]; // Mapping created - but value undefined!
std::cout << "Map size after map[\"cyrus\"]: " << map.size() << std::endl;
map["cyrus"] = "(who knows)"; // Define the value so we don't crash
// Iterator
for (auto& pair : map) {
std::cout << "> " << pair.key << " wants " << pair.value << "." << std::endl;
}
return 0;
}