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 »
Textbook page 957, question 7 (a set of items).


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, arrays, 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 array 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

Answer To: Write a template-based class that implements a set of items. A set is a collection of items in which...

Sudipta answered on Nov 27 2021
127 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 */
template
class Set
{
private:
/* vector to store set elements */
vector 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 */
bool IsMember(T item);
/* returns array of elements includes all set elements */
T* getArrayOfSetItems();
};
template
int Set::addItem(T item)
{
if (find(mElems.begin(), mElems.end(), item) == mElems.end())
{
cout << "Item is added to set.\n";
mElems.push_back(item);
return 0;
}
cout << "Item is already exists.\n";
return -1;
}
template
int 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