- Set Declaration.
- insert()
- size()
- max_size()
- begin(), end()
- empty()
- erase()
- clear()
- find();
- count()
- lower_bound()
- upper_bound()
- emplace()
- swap()
- operator()
Set Examples :
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> s; /// array
/// set<int,greater<int>> s; /// descending array
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(4);
s.insert(5);
s.insert(4);
s.insert(5);
set<int> s2;
s2.insert(11);
s2.insert(12);
s2.insert(13);
s2.insert(14);
set<int> s3 = {21, 22, 23, 24, 25};
///////// size ////////////
cout << "Size : " << s.size() << endl;
///////// max-size ////////////
cout <<"Max_Size : "<< s.max_size() << endl;
///////// count ////////////
cout << "count :" << s.count() << endl;
///////// empty ////////////
cout <<"empty : "<< s.empty() << endl;
///////// iterator ////////////
set<int>::iterator it; /// iterator
it = s.begin();
advance(it, 3); /// insert index
s.erase(it); /// delete hobe
// print array
for (auto it : s)
{
cout << it << " ";
}
/// normal iteration ///
set<int>::iterator it; /// iterator
for (it = s.begin(); it != s.end(); it++)
{
cout << *it << " ";
}
////// normal array /////
for (int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
///////// find ////////////
set<int>::iterator it; /// iterator
it = s.find(3);
if (it != s.end())
cout << "Find" << endl;
else
cout << "Not Find" << endl;
///////// count ////////////
int c = s.count(3);
if (c=1)
{
cout << "yes" << endl;
}else{
cout << "No" << endl;
}
for (auto it : s)
{
cout << it << " ";
}
///////// lower bound ////////////
set<int>::iterator it; /// iterator
it = s.lower_bound(2);
if (it == s.end())
cout << "the element is larger to the grater element" << endl;
else
cout << "the lower bound :" << *it << endl;
///////// Upper bound ////////////
set<int>::iterator it; /// iterator
it = s.upper_bound(2);
if (it == s.end())
cout << "the element is larger to the grater element" << endl;
else
cout << "the upper bound :" << *it << endl;
///////// Insert ////////////
s.insert(s.begin(),99);/// input number
it = s.begin();
advance(it, 3); /// insert index
s.insert(it, 2,9); /// input number and koi bar print hobe
///////// Erase ////////////
s.erase(s3.begin());/// erase by index number
it = s3.begin();
advance(it, 3); /// insert index
s3.insert(it, 2,9); /// input number and koi bar print hobe
///////// remove ////////////
s3.remove(22);
///////// sort ////////////
s4.sort();
///////// unique ////////////
s3.unique();
for (int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
///////// swap ////////////
swap(s2, s3);
swap(s, s2); /// swap between 2 array ;
sort(s.begin(),s.end());
sort(s.begin()+4,s.end());
reserse(s.begin(),s.end());
reserse(s.begin()+4,s.end());
for (int i = 0; i < s.size(); i++)
{
cout << s[i] << " ";
}
sector<int>::iterator it;
it = s.begin() + 5;
///////// merge ////////////
s.merge(s3);
}
0 Comments