C++ std::set equivalent for Zig

As the title says, is there any similar data structure in the Standard Library that works the same as an std::set from C++?

Yes - you can use a HashMap that has a void value type and just check against the keys. Our friend @deckarep made a project on github that implements a set object that does just this. You could use the project itself directly or just go with the map:

2 Likes

alr, thanks!

If It is set of string([]const u8), you can use std.BufSet.

https://ziglang.org/documentation/master/std/#std.BufSet

If it is set of enum, you can use std.enums.EnumSet

https://ziglang.org/documentation/master/std/#std.EnumSet

And std.Treap is a tree algorithm implementation for zig

https://ziglang.org/documentation/master/std/#std.Treap

If those values is all string literal, std.StaticStringMap passing void as the parameter is also useful.

https://ziglang.org/documentation/master/std/#std.StaticStringMap

example of StaticStringMap

const map = std.StaticStringMap(void).initComptime(.{
    .{"foo"},
    .{"bar"},
    .{"baz"}.
});
2 Likes