summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-01-12 10:55:35 +0100
committerTyge Løvset <[email protected]>2022-01-12 10:55:35 +0100
commitd616e7db3a2325646e8647cdc433b12e9438c251 (patch)
tree69cbc1d259ba7ca01c01ef20e0179c1e59fdfaf1 /examples
parentf69fb08603aca020e8d855b0469809982bb67322 (diff)
downloadSTC-modified-d616e7db3a2325646e8647cdc433b12e9438c251.tar.gz
STC-modified-d616e7db3a2325646e8647cdc433b12e9438c251.zip
Docs update, mmap.c example update.
Diffstat (limited to 'examples')
-rw-r--r--examples/mmap.c35
1 files changed, 21 insertions, 14 deletions
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 <stc/clist.h>
+// 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<int>
#include <stc/csmap.h>
-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);
}
}