A bag can contain more than one copy of an item For example

A bag can contain more than one copy of an item. For example, the chapter describes a bag that contains the number 4 and two copies of the number 8. This bag behavior is different from a set, which can contain only a single copy of any given item. Write a new container class called set, which is similar to a bag, except that a set can contain only one copy of any given item. You\'ll need to change the interface a bit. For example, instead of the bag\'s count function, you\'ll want a constant member function such as this:

bool set::contains

(const value_type& target) const;

//Postcondition: The return value is true if target is in the set; otherwise the return value is false.

Make an explicit statement of the invariant of the set class. Do a time analysis for each operation. At this point, an efficient implementation is not needed. For example, just adding a new item to a set will take linear time because you\'ll need to check that the new item isn\'t already present.

You may also want to add additional operations to your set class, such as an operator for subtraction, but the program must implement set operations union, intersection, and relative complement.

Solution

#include <iostream>

#include <assert.h>

#include <stdlib.h>

#include \"set.h\" void set::insert(const Item& entry)

{

assert(size() &lt;

CAPACITY);

//set::const;

if(contains(entry==false))

{ data[used] = entry; used++;

}

}

bool set::contains(const Item& target)const { for(int i = 0;

i &lt;

used;

i++ ) { if(data[i]==target) return true;

}

return false;

}

void set::display() { if(used==0) { cout&lt;

&lt;

\"The bag is empty.\"&lt;

&lt;

endl;

}

else{ for(size_t i=0; i&lt;

used;

&gt;

cout&lt;

&lt;

data[i]&lt;

&lt;\" \"; }

A bag can contain more than one copy of an item. For example, the chapter describes a bag that contains the number 4 and two copies of the number 8. This bag be
A bag can contain more than one copy of an item. For example, the chapter describes a bag that contains the number 4 and two copies of the number 8. This bag be

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site