Posted on

Basics

Sets are a part of the C++ STL. Sets are containers that store unique elements following a specific order. Here are some of the frequently used member functions of sets:

  • Declaration:set<int>s; //Creates a set of integers.
  • Size:int length=s.size(); //Gives the size of the set.
  • Insert:s.insert(x); //Inserts an integer x into the set s.
  • Erasing an element:s.erase(val); //Erases an integer val from the set s.
  • Finding an element:set<int>::iterator itr=s.find(val); //Gives the iterator to the element val if it is found otherwise returns s.end() . Ex: set<int>::iterator itr=s.find(100); //If 100 is not present then it==s.end().

Example

#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 */   
    int Q;
    cin >> Q;
    set<int> s;
    for(int i = 0; i < Q; i++) {
        int type, num;
        cin >> type >> num;

        switch(type) {
            case 1: s.insert(num); break;
            case 2: s.erase(num); break;
            case 3: 
                if(s.find(num) == s.end()) cout << "No\n";
                else cout << "Yes\n";
                break;
        }
    }
    return 0;
}



Leave a Reply

Your email address will not be published. Required fields are marked *