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}
1#include<iostream>
2#include<set>
3
4using namespace std;
5
6int main(){
7
8 // Set with values
9 set<int, greater<int>> s1 = {6, 10, 5, 1};
10 // s1 = {10, 6, 5, 1}
11
12 // Inserting elements in the set
13 s1.insert(12);
14 s1.insert(20);
15 s1.insert(3);
16
17 // Iterator for the set
18 set<int> :: iterator it;
19
20 // Print the elements of the set
21 for(it=s1.begin(); it != s1.end();it++)
22 cout<<*it<<" ";
23 cout<<endl;
24
25}
1#include<iostream>
2#include<set>
3
4using namespace std;
5
6int main(){
7
8 // Set with values
9 set<int, greater<int>> s1 = {6, 10, 5, 1};
10
11 // Iterator for the set
12 set<int> :: iterator it;
13
14 // Print the elements of the set
15 for(it=s1.begin(); it != s1.end();it++)
16 cout<<*it<<" ";
17 cout<<endl;
18}
1// constructing sets
2#include <iostream>
3#include <set>
4
5bool fncomp (int lhs, int rhs) {return lhs<rhs;}
6
7struct classcomp {
8 bool operator() (const int& lhs, const int& rhs) const
9 {return lhs<rhs;}
10};
11
12int main ()
13{
14 std::set<int> first; // empty set of ints
15
16 int myints[]= {10,20,30,40,50};
17 std::set<int> second (myints,myints+5); // range
18
19 std::set<int> third (second); // a copy of second
20
21 std::set<int> fourth (second.begin(), second.end()); // iterator ctor.
22
23 std::set<int,classcomp> fifth; // class as Compare
24
25 bool(*fn_pt)(int,int) = fncomp;
26 std::set<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare
27
28 return 0;
29}