Why use set.find (x)! = Set.end () when searching for an element.

I am wondering what is wrong when I use *(set.find(x)) == x instead set.find(x)!=set.end(). It usually works, but when trying to ask a question about Hackerrank (question: link ). This code gives CA for all test cases:

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */   

set<int>s;
int n,x,y;
cin >> n;
while(n--){
    cin >> y >> x;
    if(y==1)
        s.insert(x);
    else if(y==2)
        s.erase(x);
    else {
        set<int>::iterator it=s.find(x);
        if(it != s.end())
            cout << "Yes" <<endl;
        else cout << "No" <<endl;
    }
}
return 0;}

But this does not work for 2 test cases. The test file file is too large, and it makes no sense to try to check this huge file.: -

 #include <cmath>
 #include <cstdio>
 #include <vector>
 #include <iostream>
 #include <set>
 #include <algorithm>
 using namespace std;


 int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */

set<int>s;
int n,x,y;
cin >> n;
while(n--){
    cin >> y >> x;
    if(y==1)
        s.insert(x);
    else if(y==2)
        s.erase(x);
    else {
        set<int>::iterator it=s.find(x);
        if(*it==x)
            cout << "Yes" <<endl;
        else cout << "No" <<endl;
    }
}
return 0;
}
+4
source share
2 answers

, find end, , undefined, , - , , . ++. end " ", , ; .

, , set.count(x).

+10

,

10

19268
3 7401  //first search without any input

3

set<int>::iterator it=s.find(x); //Returns end iterator

end(),

if(*it==x)
0

Source: https://habr.com/ru/post/1681514/


All Articles