From d616e7db3a2325646e8647cdc433b12e9438c251 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 12 Jan 2022 10:55:35 +0100 Subject: Docs update, mmap.c example update. --- examples/mmap.c | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) (limited to 'examples') diff --git a/examples/mmap.c b/examples/mmap.c index ea08017b..289da616 100644 --- a/examples/mmap.c +++ b/examples/mmap.c @@ -1,18 +1,20 @@ // This implements the multimap c++ example found at: // https://en.cppreference.com/w/cpp/container/multimap/insert -// Map of int => clist_str. Note the negation of c_default_cmp +// Multimap entries #define i_val_str #include +// Map of int => clist_str. #define i_type Multimap #define i_key int -#define i_val_bind clist_str -#define i_cmp -c_default_cmp +#define i_val_bind clist_str // uses clist_str as i_val and binds clist_str_clone, clist_str_drop +#define i_cmp -c_default_cmp // like std::greater #include -void print(const Multimap mmap) +void print(const char* lbl, const Multimap mmap) { + printf("%s ", lbl); c_foreach (e, Multimap, mmap) { c_foreach (s, clist_str, e.ref->second) printf("{%d,%s} ", e.ref->first, s.ref->str); @@ -31,36 +33,41 @@ int main() c_auto (Multimap, mmap) { // list-initialize - struct {int i; const char* s;} vals[] = {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}; - c_forrange (i, c_arraylen(vals)) insert(&mmap, vals[i].i, vals[i].s); + struct { int first; const char* second; } vals[] = + {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}}; + c_forrange (i, c_arraylen(vals)) insert(&mmap, c_pair(vals[i])); + print("#1", mmap); // insert using value_type insert(&mmap, 5, "pqr"); - print(mmap); + print("#2", mmap); // insert using make_pair insert(&mmap, 6, "uvw"); - print(mmap); + print("#3", mmap); insert(&mmap, 7, "xyz"); - print(mmap); + print("#4", mmap); // insert using initialization_list insert(&mmap, 5, "one"); insert(&mmap, 5, "two"); - print(mmap); + print("#5", mmap); + // FOLLOWING NOT IN ORIGINAL EXAMPLE: + // erase all entries with key 5 Multimap_erase(&mmap, 5); - print(mmap); + print("+6", mmap); - // find and erase a specific entry + // find and erase first entry containing "bar" clist_str_iter pos; - c_foreach (e, Multimap, mmap) + c_foreach (e, Multimap, mmap) { if ((pos = clist_str_find(&e.ref->second, "bar")).ref != clist_str_end(&e.ref->second).ref) { clist_str_erase_at(&e.ref->second, pos); break; } - print(mmap); + } + print("+7", mmap); } } -- cgit v1.2.3