So I’m working on a project and I have to use the set library on class objects. Those objects have many attributes, ID being one of them.
What I wanted to do was search for an object inside a “set” by its ID. The problem is set only has find and I don’t know how to search for an ID this way since I’d have to use find(class object) and not find(int). I tried messing with class operators to read it as an object but couldn’t find a way.
Also, I thought about algorithm::find_if, but that would just check every element from beggining to end instead of using the set “tree” search functions, right?
Thanks in advance.
You need to create a constructor for your class that takes
intas its only argument. Doing so allows implicit conversion fromintto your class, making it possible to callstd::set::find(int), as requested.For example:
This yields: