1#include <bits/stdc++.h>
2#include <iostream>
3#include <vector>
4#include <algorithm>
5#include <set>
6
7using namespace std;
8//set mentains internally the ascending order of these numbers
9void setDemo()
10{
11 set<int> S;
12 S.insert(1);
13 S.insert(2);
14 S.insert(-1);
15 S.insert(-10);
16 S.erase(1);//to remove an element
17
18 //Print all the values of the set in ascending order
19 for(int x:S){
20 cout<<x<<" ";
21 }
22
23 //check whether an element is present in a set or not
24 auto it = S.find(-1);//this will return an iterator to -1
25 //if not present it will return an iterator to S.end()
26
27 if (it == S.end()){
28 cout<<"not Present\n";
29 }else{
30 cout <<" present\n";
31 cout << *it <<endl;
32 }
33 //iterator to the first element in the set which is
34 //greater than or equal to -1
35 auto it2 = S.lower_bound(-1);
36 //for strictly greater than -1
37 auto it3 = S.upper_bound(-1);
38 //print the contents of both the iterators
39 cout<<*it2<<" "<<*it3<<endl;
40}
41
42int main() {
43 setDemo();
44 return 0;
45}
1set<int, less<int>> st;
2// or
3set<int, greater<int>> st;
4// c++ 11
5auto cmp = [](int a, int b) { return ... };
6set<int, decltype(cmp)> s(cmp);