Great Deal! Get Instant $25 FREE in Account on First Order + 10% Cashback on Every Order Order Now

Write a template-based class that implements a set of items. A set is a collection of items in which no item occurs more than once. Internally, you may represent the set using the data structure of...

1 answer below »
Write a template-based class that implements a set of items. A set is a collection of items in which no item occurs more than once. Internally, you may represent the set using the data structure of your choice (for example, list, vector, a
ays, etc.). However, the class should externally support the
following functions:
1. Add a new item to the set. If the item is already in the set, then nothing
happens.
2. Remove an item from the set.
3. Return the number of items in the set.
4. Determine if an item is a member of the set.
5. Return a pointer to a dynamically created a
ay containing each item in the set. The caller of this function is responsible for deallocating the memory. Test your class by creating different sets of different data types (for example, strings, integers, or other classes). If you add objects to your set, then you may need to overload the == and != operators for the object’s class so your template-based set class can properly determine membership.
Answered Same DayNov 26, 2021

Solution

Sudipta answered on Nov 27 2021
53 Votes
There are two solutions I wrote for you. Hope that helps. Check each and submit the one which you feel best.
ANSWER 1:
Set.hpp
#include #include using namespace std;
* Note:
* For creation of set of objects, the operators == and != should be
* overloaded for object's class.
*
* Class which defines the functionality of set *
templateclass Set
{
private:
* vector to store set elements *
vecto
T> mElems;
public:
* function to add new set item *
int addItem(T item);
* function to remove item from the set *
int removeItem(T item);
* returns number of items in the set *
int numItems();
* returns true, if given item is in set *
ool IsMember(T item);
* returns a
ay of elements includes all set elements *
T* getA
ayOfSetItems();
};
templateint Set::addItem(T item)
{
if (find(mElems.begin(), mElems.end(), item) == mElems.end())
{
cout
"Item is added to set.\n";
mElems.push_back(item);
eturn 0;
}
cout
"Item is already exists.\n";
eturn -1;
}
templateint Set::removeItem(T item)
{
if (find(mElems.begin(), mElems.end(), item) != mElems.end())
{
cout
"Item is removed from...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here