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

前端 未结 10 1990
春和景丽
春和景丽 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:26

    I use

    if(!my_set.count(that_element)) //Element is present...
    ;
    

    But it is not as efficient as

    if(my_set.find(that_element)!=my_set.end()) ....;
    

    My version only saves my time in writing the code. I prefer it this way for competitive coding.

提交回复
热议问题