summaryrefslogtreecommitdiffhomepage
path: root/docs/cmap_api.md
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-12-01 21:45:19 +0100
committerTyge Løvset <[email protected]>2020-12-01 21:45:19 +0100
commitae032cb11e32f08258dd78c23e7624663cd25ef6 (patch)
tree7f9657c6c8dff0ab274cb713d35fbf3e96835daf /docs/cmap_api.md
parent65033a7da71abb322b3dacf3e4a160961972c39e (diff)
downloadSTC-modified-ae032cb11e32f08258dd78c23e7624663cd25ef6.tar.gz
STC-modified-ae032cb11e32f08258dd78c23e7624663cd25ef6.zip
Added some examples.
Diffstat (limited to 'docs/cmap_api.md')
-rw-r--r--docs/cmap_api.md65
1 files changed, 34 insertions, 31 deletions
diff --git a/docs/cmap_api.md b/docs/cmap_api.md
index 9c90dbdb..1fb5994c 100644
--- a/docs/cmap_api.md
+++ b/docs/cmap_api.md
@@ -126,42 +126,45 @@ uint32_t c_default_hash16(const void *data, size_t len);
uint32_t c_default_hash32(const void* data, size_t len);
```
-Example:
+## Example
```c
#include <stdio.h>
-#include <stc/cmap.h>
#include <stc/cstr.h>
+#include <stc/cmap.h>
-using_cmap(ii, int, int);
using_cmap_str();
-int main() {
- // -- map of ints --
- cmap_ii nums = cmap_ii_init();
- cmap_ii_put(&nums, 8, 64); // similar to insert_or_assign()
- cmap_ii_emplace(&nums, 11, 121);
-
- printf("%d\n", cmap_ii_find(&nums, 8)->second);
- cmap_ii_del(&nums);
-
- // -- map of cstr_t --
- cmap_str strings = cmap_str_init();
- cmap_str_emplace(&strings, "Make", "my");
- cmap_str_emplace(&strings, "Rainy", "day");
- cmap_str_emplace(&strings, "Sunny", "afternoon");
- c_push_items(&strings, cmap_str, { {"Eleven", "XI"}, {"Six", "VI"} });
-
- printf("size = %zu\n", cmap_str_size(strings));
- c_foreach (i, cmap_str, strings)
- printf("%s: %s\n", i.val->first.str, i.val->second.str);
- cmap_str_del(&strings); // frees all strings and map.
+int main()
+{
+ // Create an unordered_map of three strings (that map to strings)
+ cmap_str u = cmap_inits;
+ c_push_items(&u, cmap_str, {
+ {"RED","#FF0000"},
+ {"GREEN","#00FF00"},
+ {"BLUE","#0000FF"}
+ });
+
+ // Iterate and print keys and values of unordered map
+ c_foreach (n, cmap_str, u) {
+ printf("Key:[%s] Value:[%s]\n", n.val->first.str, n.val->second.str);
+ }
+
+ // Add two new entries to the unordered map
+ cmap_str_put(&u, "BLACK", "#000000");
+ cmap_str_put(&u, "WHITE", "#FFFFFF");
+
+ // Output values by key
+ printf("The HEX of color RED is:[%s]\n", cmap_str_at(&u, "RED")->str);
+ printf("The HEX of color BLACK is:[%s]\n", cmap_str_at(&u, "BLACK")->str);
+
+ return 0;
}
-// Output:
-64
-size = 5
-Rainy: day
-Sunny: afternoon
-Six: VI
-Make: my
-Eleven: XI
+```
+Output:
+```
+Key:[RED] Value:[#FF0000]
+Key:[GREEN] Value:[#00FF00]
+Key:[BLUE] Value:[#0000FF]
+The HEX of color RED is:[#FF0000]
+The HEX of color BLACK is:[#000000]
```