How to check that an element is in a std::set?

fulmicoton picture fulmicoton · Nov 9, 2009 · Viewed 334.1k times · Source

How do you check that an element is in a set?

Is there a simpler equivalent of the following code:

myset.find(x) != myset.end()

Answer

unwind picture unwind · Nov 9, 2009

The typical way to check for existence in many STL containers such as std::map, std::set, ... is:

const bool is_in = container.find(element) != container.end();