summaryrefslogtreecommitdiffhomepage
path: root/examples/unordered_set.c
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-10-26 20:46:43 +0200
committerTyge Løvset <[email protected]>2021-10-26 20:46:43 +0200
commit0c6b41fd08bac55281ff9ad41b0d052171881651 (patch)
tree4a8107ce800eba0bd0d659d557133466e01da79c /examples/unordered_set.c
parent0f95c005891b0d4b3b2610fdba241cf78c372d81 (diff)
downloadSTC-modified-0c6b41fd08bac55281ff9ad41b0d052171881651.tar.gz
STC-modified-0c6b41fd08bac55281ff9ad41b0d052171881651.zip
Added some examples and updated docs. Removed cvec_X_erase() and cdeq_X_erase() - may be used wrong. Use cvec_X_erase_n() instead.
Fixed type-bug in cpque.h (same was in cqueue.h).
Diffstat (limited to 'examples/unordered_set.c')
-rw-r--r--examples/unordered_set.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/unordered_set.c b/examples/unordered_set.c
new file mode 100644
index 00000000..f4e37cf8
--- /dev/null
+++ b/examples/unordered_set.c
@@ -0,0 +1,41 @@
+// https://iq.opengenus.org/containers-cpp-stl/
+// C program to demonstrate various function of stc cset
+#define i_key_str
+#include <stc/cset.h>
+
+int main()
+{
+ // declaring set for storing string data-type
+ c_auto (cset_str, stringSet)
+ {
+ // inserting various string, same string will be stored
+ // once in set
+ cset_str_emplace(&stringSet, "code");
+ cset_str_emplace(&stringSet, "in");
+ cset_str_emplace(&stringSet, "C");
+ cset_str_emplace(&stringSet, "is");
+ cset_str_emplace(&stringSet, "fast");
+
+ const char* key = "slow";
+
+ // find returns end iterator if key is not found,
+ // else it returns iterator to that key
+
+ if (cset_str_find(&stringSet, key).ref == cset_str_end(&stringSet).ref)
+ printf("\"%s\" not found\n", key);
+ else
+ printf("Found \"%s\"\n", key);
+
+ key = "C";
+ if (!cset_str_contains(&stringSet, key))
+ printf("\"%s\" not found\n", key);
+ else
+ printf("Found \"%s\"\n", key);
+
+ // now iterating over whole set and printing its
+ // content
+ printf("All elements :\n");
+ c_foreach (itr, cset_str, stringSet)
+ printf("%s\n", itr.ref->str);
+ }
+}