Home >Backend Development >C++ >C++ program for inserting, deleting and searching in set STL
Suppose we have a collection data structure of integer type data. On our standard input, we provide n queries. In each query (each row) we have two elements. The first is the operator and the second is the element. The operation is as follows -
Insert. This will insert the element into the collection
remove it. This will remove the element from the collection if it exists
Search. This will search the collection for the element and display "Yes" if present, otherwise "No".
So if the input is something like n = 7, then the query = [[1,5],[1,8],[1,3],[2,8], [1,9],[3,8],[3,3]], then the output will be [No, Yes] because 8 does not exist in the set, but 3 does.
To solve this problem, we will follow the following steps -
#include <iostream> #include <set> using namespace std; int main(){ set<int> s; set<int>::iterator it; int q,x; int qt; cin >> q; while(q--){ cin>>qt>>x; switch(qt){ case 1:s.insert(x); break; case 2:s.erase(x); break; case 3:it=s.find(x); if(it==s.end()) cout<<"No"<<endl; else cout<<"Yes"<<endl; break; } } return 0; }Input
7 1 5 1 8 1 3 2 8 1 9 3 8 3 3
No Yes
The above is the detailed content of C++ program for inserting, deleting and searching in set STL. For more information, please follow other related articles on the PHP Chinese website!