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

前端 未结 10 1958
春和景丽
春和景丽 2020-11-30 17:13

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()
10条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 17:39

    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();
    

提交回复
热议问题