set in c++
#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
//set mentains internally the ascending order of these numbers
void setDemo()
{
set<int> S;
S.insert(1);
S.insert(2);
S.insert(-1);
S.insert(-10);
S.erase(1);//to remove an element
//Print all the values of the set in ascending order
for(int x:S){
cout<<x<<" ";
}
//check whether an element is present in a set or not
auto it = S.find(-1);//this will return an iterator to -1
//if not present it will return an iterator to S.end()
if (it == S.end()){
cout<<"not Present\n";
}else{
cout <<" present\n";
cout << *it <<endl;
}
//iterator to the first element in the set which is
//greater than or equal to -1
auto it2 = S.lower_bound(-1);
//for strictly greater than -1
auto it3 = S.upper_bound(-1);
//print the contents of both the iterators
cout<<*it2<<" "<<*it3<<endl;
}
int main() {
setDemo();
return 0;
}
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
// empty set container
set <int, greater <int> > s1;
// insert elements in random order
s1.insert(40);
s1.insert(30);
s1.insert(60);
s1.insert(20);
s1.insert(50);
s1.insert(50); // only one 50 will be added to the set
s1.insert(10);
// printing set s1
set <int, greater <int> > :: iterator itr;
cout << "\nThe set s1 is : ";
for (itr = s1.begin(); itr != s1.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
// assigning the elements from s1 to s2
set <int> s2(s1.begin(), s1.end());
// print all elements of the set s2
cout << "\nThe set s2 after assign from s1 is : ";
for (itr = s2.begin(); itr != s2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
// remove all elements up to 30 in s2
cout << "\ns2 after removal of elements less than 30 : ";
s2.erase(s2.begin(), s2.find(30));
for (itr = s2.begin(); itr != s2.end(); ++itr)
{
cout << '\t' << *itr;
}
// remove element with value 50 in s2
int num;
num = s2.erase (50);
cout << "\ns2.erase(50) : ";
cout << num << " removed \t" ;
for (itr = s2.begin(); itr != s2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
//lower bound and upper bound for set s1
cout << "s1.lower_bound(40) : "
<< *s1.lower_bound(40) << endl;
cout << "s1.upper_bound(40) : "
<< *s1.upper_bound(40) << endl;
//lower bound and upper bound for set s2
cout << "s2.lower_bound(40) : "
<< *s2.lower_bound(40) << endl;
cout << "s2.upper_bound(40) : "
<< *s2.upper_bound(40) << endl;
return 0;
}
#include <iostream>
#include <set>
#include <iterator>
using namespace std;
int main()
{
// empty set container
set <int, greater <int> > gquiz1;
// insert elements in random order
gquiz1.insert(40);
gquiz1.insert(30);
gquiz1.insert(60);
gquiz1.insert(20);
gquiz1.insert(50);
gquiz1.insert(50); // only one 50 will be added to the set
gquiz1.insert(10);
// printing set gquiz1
set <int, greater <int> > :: iterator itr;
cout << "\nThe set gquiz1 is : ";
for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
// assigning the elements from gquiz1 to gquiz2
set <int> gquiz2(gquiz1.begin(), gquiz1.end());
// print all elements of the set gquiz2
cout << "\nThe set gquiz2 after assign from gquiz1 is : ";
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
// remove all elements up to 30 in gquiz2
cout << "\ngquiz2 after removal of elements less than 30 : ";
gquiz2.erase(gquiz2.begin(), gquiz2.find(30));
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}
// remove element with value 50 in gquiz2
int num;
num = gquiz2.erase (50);
cout << "\ngquiz2.erase(50) : ";
cout << num << " removed \t" ;
for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr)
{
cout << '\t' << *itr;
}
cout << endl;
//lower bound and upper bound for set gquiz1
cout << "gquiz1.lower_bound(40) : "
<< *gquiz1.lower_bound(40) << endl;
cout << "gquiz1.upper_bound(40) : "
<< *gquiz1.upper_bound(40) << endl;
//lower bound and upper bound for set gquiz2
cout << "gquiz2.lower_bound(40) : "
<< *gquiz2.lower_bound(40) << endl;
cout << "gquiz2.upper_bound(40) : "
<< *gquiz2.upper_bound(40) << endl;
return 0;
}
// constructing sets
#include <iostream>
#include <set>
bool fncomp (int lhs, int rhs) {return lhs<rhs;}
struct classcomp {
bool operator() (const int& lhs, const int& rhs) const
{return lhs<rhs;}
};
int main ()
{
std::set<int> first; // empty set of ints
int myints[]= {10,20,30,40,50};
std::set<int> second (myints,myints+5); // range
std::set<int> third (second); // a copy of second
std::set<int> fourth (second.begin(), second.end()); // iterator ctor.
std::set<int,classcomp> fifth; // class as Compare
bool(*fn_pt)(int,int) = fncomp;
std::set<int,bool(*)(int,int)> sixth (fn_pt); // function pointer as Compare
return 0;
}