summaryrefslogtreecommitdiffhomepage
path: root/examples/unordered_set.c
blob: 9ecd59feeff1052fed6013899e7426be70b92dca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// https://iq.opengenus.org/containers-cpp-stl/
// C program to demonstrate various function of stc cset
#define i_implement
#include <stc/cstr.h>
#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", cstr_str(itr.ref));
    }
}