From c151e165260dffafdad33ee74f277137ce140fb2 Mon Sep 17 00:00:00 2001 From: tidwall Date: Tue, 22 Mar 2022 10:45:05 -0700 Subject: [PATCH] Add hashmap_iter example --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2c153d6..78cd474 100644 --- a/README.md +++ b/README.md @@ -68,9 +68,16 @@ int main() { user = hashmap_get(map, &(struct user){ .name="Tom" }); printf("%s\n", user?"exists":"not exists"); - printf("\n-- iterate over all users --\n"); + printf("\n-- iterate over all users (hashmap_scan) --\n"); hashmap_scan(map, user_iter, NULL); + size_t iter = 0; + void *item; + while (hashmap_iter(map, &iter, &item)) { + const struct user *user = item; + printf("%s (age=%d)\n", user->name, user->age); + } + hashmap_free(map); } @@ -81,7 +88,7 @@ int main() { // Dale age=44 // not exists // -// -- iterate over all users -- +// -- iterate over all users (hashmap_scan) -- // Dale (age=44) // Roger (age=68) // Jane (age=47) @@ -104,7 +111,8 @@ hashmap_clear # clear the hash map ### Iteration ```sh -hashmap_scan # iterate over items in hash map +hashmap_iter # loop based iteration over all items in hash map +hashmap_scan # callback based iteration over all items in hash map ``` ### Hash helpers