```
#include "set.h"
#include <iterator>
using namespace std;
set::set()
{
cardinality = 0;
setlist.resize(DEFAULTSIZE, false);
}
set::set(int n)
{
cardinality = n;
setlist.resize(n, false);
}
bool set::empty() const
{
if ( setlist.empty() )
{
return true;
}
return false;
}
void set::insert(int x)
{
if (x >= 0 && x <= DEFAULTSIZE)
{
setlist.insert(setlist.end(), 1, x);
cardinality++;
}
}
int set::getLargest() const
{
if ( !setlist.empty() )
{
return *( max_element( setlist.begin(), setlist.end() ) );
}
}
int set::getSmallest() const
{
if ( !setlist.empty() )
{
return *( min_element( setlist.begin(), setlist.end() ) );
}
}
void set::dump() const
{
copy (setlist.begin(), setlist.end(), ostream_iterator<int>(cout," "));
cout << endl;
}
bool set::isIn(int x) const
{
return *( find( setlist.begin(), setlist.end(), x) );
}
```

```
#ifndef SETH
#define SETH
// definition of a set of integers
#include <iostream>
#include <vector>
using namespace std;
const int DEFAULTSIZE = 100;
class set
{
public :
set(); // default constructor - constructs an empty set of integers in range 0 to DEFAULTSIZE -1
set(int n);
// pre : n >= 0 and n <= 500000
// post : constructs an empty set of integers in range 0 to n-1
bool empty() const;
// pre : none
// post : returns true if set empty and otherwise false
bool isIn(int x) const;
// pre : none
// post : returns true if x is in set and otherwise false
void dump() const;
// pre : none
// post : dumps out the valus in set (separated by white space)
int getCardinality() const;
// pre : none
// post : returns the number of elements in the set
void insert(int x);
// pre : none
// post : inserts x into this set (if in range)
int getSmallest() const;
// pre : set is not empty
// post : returns smallest element in the set
int getLargest() const;
// pre : set is not empty
// post : returns largest element in the set
set unions(int x) const;
// pre : none
// post : returns the union of this set and x (if x in range)
set unions(const set &other) const;
// pre : both sets are in same range (domain)
// post : returns the union of this set and other set
set intersect(int x) const;
// pre : none
// post : returns the intersection of this set and x (if x in range)
set intersect(const set &other) const;
// pre : both sets are in same range (domain)
// post : returns the intersection of this set and other set
void display()const;
// pre : none
// displays the elements of this set enclosed in braces and
// separated by commas eg {1,2,3} or {} (do not use backspace, delete etc)
set difference(const set &other) const;
// pre : both sets are in same range (domain)
// post : returns the difference of this set and other set
set difference(int x)const;
// pre : none
// post : returns the difference of this set and x (if x in range)
int getUpperRange()const;
// pre : none
// post : returns the upper value in the domain (upper range)
bool isEqual (const set & other) const;
// pre : both sets are in the same range (domain)
// post : returns true if this set is equal to other set
private:
vector <bool> setlist; // indicates set elements as true
int cardinality; // number of items in set
};
#endif
```

```
#include "set.h"
#include <iostream>
using namespace std;
void printMenu();
int main()
{
char choice = '#';
int element;
set myset;
cout << " This program can be used for testing basic set implementation " << endl;
while (choice != 'Q')
{
printMenu();
cin >> choice;
choice = toupper(choice);
switch (choice)
{
case 'I' : cout << " Enter int element " << endl;
if (cin >> element)
myset.insert(element);
break;
case 'E' : if (myset.empty()) cout << "Set is empty" << endl;
else cout << "Set is not empty " << endl;
break;
case 'L' : if (!myset.empty()) cout << "Largest is " << myset.getLargest() << endl; break;
case 'S' : if (!myset.empty()) cout << "Smallest is " << myset.getSmallest() << endl; break;
case 'D' : myset.dump(); break;
//case 'P' : myset.display(); break;
case 'F' : cout << "Enter int element " << endl;
if ((cin >> element) && myset.isIn(element))
cout << element <<" is in set " << endl;
else cout << element << " is not in set " << endl; break;
case 'Q' : break;
default : cout << "Invalid choice " << endl;
}
}
system("pause");
return 0;
}
void printMenu()
{
cout << "\t Testing set " << endl;
cout << " I : Insert an element into the set " << endl;
cout << " E : test if set is Empty" << endl;
cout << " L : show Largest element in set " << endl;
cout << " S : show Smallest element in set " << endl;
cout << " D : Dump the elements in set " << endl;
cout << " F : Find if element x is in set " << endl;
cout << " P : Pretty display of elements in set " << endl;
cout << " Q : Quit " << endl;
cout << endl;
}
```