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::iterator itr=s.find(val); //Gives the iterator to the element val if it is found otherwise returns s.end() . Ex: set::iterator itr=s.find(100); //If 100 is not present then it==s.end().
for(set::iterator itr = s.begin(); itr != s.end(); itr++){
cout<<*itr<<endl; // prints the element in the set
}
Multisets are a type of associative containers similar to set, with an exception that multiple elements can have same values.
multiset<int> ms;
Removing Element from multiset which have a value multiple times
ms.erase(val) – Remove all instance of val from multiset
ms.erase(ms.find(val)) – Remove only one instance of val from multiset
Example
#include <bits/stdc++.h>
using namespace std;
int main()
{
multiset<int> ms;
ms.insert(10);
ms.insert(10);
ms.insert(10);
// it will give output 3
cout << ms.count(10) << endl;
// removing single instance from multiset
// it will remove only one value of 10 from multiset
ms.erase(a.find(10));
// it will give output 2
cout << ms.count(10) << endl;
// removing all instance of element from multiset it will remove all instance of value 10
ms.erase(10);
// it will give output 0 because all instance of value is removed from mulitset
cout << a.count(10) << endl;
return 0;
}
All the member function of the multisets are same as sets.
Try the following example in the editor below.
You will be given Q
queries. Each query is one of the following three types:
x
from the set. (If the number x
is not present in the set, then do nothing).x
is present in the set, then print "Yes"(without quotes) else print "No"(without quotes).Alfter processing all queries, if the size of set is greater than 0, print each element (sorted order) in the new line.
Input Format:
First line of the input contains an integer Q denoting the number of queries.
Next Q lines consists of two integers y and x where y is the type of the query and x is an integer.
Constraints
1 <= Q <= 105 1 <= y <= 3
1 <= x <= 109
Sample Input
7 1 5
1 4
1 6
1 5
3 4
2 4
3 4
Sample Output
Yes
No 5
6