diff options
| author | Tyge Løvset <[email protected]> | 2021-05-23 14:05:40 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-05-23 14:05:40 +0200 |
| commit | 771a8dff31b0c0a905c57ec6f590cd03975f8817 (patch) | |
| tree | ffc6d482d84cdc8ac5b7b8722f192ce023cc49c2 /examples | |
| parent | 7472580ee0159d7164bf2a219c292cd060b904a2 (diff) | |
| download | STC-modified-771a8dff31b0c0a905c57ec6f590cd03975f8817.tar.gz STC-modified-771a8dff31b0c0a905c57ec6f590cd03975f8817.zip | |
Added csmap_find.c example.
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/csmap_find.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/examples/csmap_find.c b/examples/csmap_find.c new file mode 100644 index 00000000..d32cd1b5 --- /dev/null +++ b/examples/csmap_find.c @@ -0,0 +1,65 @@ +// This implements the c++ std::map::find example at:
+// https://docs.microsoft.com/en-us/cpp/standard-library/map-class?view=msvc-160#example-17
+#include <stc/csmap.h>
+#include <stc/cvec.h>
+#include <stc/cstr.h>
+#include <stdio.h>
+
+using_csmap_strval(istr, int);
+using_cvec(istr, csmap_istr_rawvalue_t, c_no_compare);
+
+void print_elem(csmap_istr_rawvalue_t p) {
+ printf("(%d, %s) ", p.first, p.second);
+}
+
+#define using_print_collection(CX) \
+ void print_collection_##CX(CX t) { \
+ printf("%zu elements: ", CX##_size(t)); \
+ \
+ c_foreach (p, CX, t) { \
+ print_elem(CX##_value_toraw(p.ref)); \
+ } \
+ puts(""); \
+ }
+
+using_print_collection(csmap_istr);
+using_print_collection(cvec_istr);
+
+
+void findit(csmap_istr c, csmap_istr_key_t val) {
+ printf("Trying find() on value %d\n", val);
+ csmap_istr_value_t* result = csmap_istr_get(&c, val); // easier with get than find.
+ if (result) {
+ printf("Element found: "); print_elem(csmap_istr_value_toraw(result)); puts("");
+ } else {
+ puts("Element not found.");
+ }
+}
+
+int main()
+{
+ c_withvar (csmap_istr, m1)
+ c_withvar (cvec_istr, v) {
+ c_emplace(csmap_istr, m1, { { 40, "Zr" }, { 45, "Rh" } });
+ puts("The starting map m1 is (key, value):");
+ print_collection_csmap_istr(m1);
+
+ typedef cvec_istr_value_t pair;
+ cvec_istr_emplace_back(&v, (pair){43, "Tc"});
+ cvec_istr_emplace_back(&v, (pair){41, "Nb"});
+ cvec_istr_emplace_back(&v, (pair){46, "Pd"});
+ cvec_istr_emplace_back(&v, (pair){42, "Mo"});
+ cvec_istr_emplace_back(&v, (pair){44, "Ru"});
+ cvec_istr_emplace_back(&v, (pair){44, "Ru"}); // attempt a duplicate
+
+ puts("Inserting the following vector data into m1:");
+ print_collection_cvec_istr(v);
+ c_foreach (i, cvec_istr, v) csmap_istr_emplace(&m1, i.ref->first, i.ref->second);
+
+ puts("The modified map m1 is (key, value):");
+ print_collection_csmap_istr(m1);
+ puts("");
+ findit(m1, 45);
+ findit(m1, 6);
+ }
+}
\ No newline at end of file |
