Question
C++ Implement operators for - and -= for the bag class. For two bags X and Y, the X-Y contains all the items of X,
C++
Implement operators for - and -= for the bag class. For two bags X and Y, the X-Y contains all the items of X, with all items from Y removed. For example, suppose that X has seven copies of number 3, and Y has two copies of the number 3. Then X-Y will have five copies of the number 3. In the case when Y has more copies of an item then X does, the bag X-Y will have no copies of that item. For example, suppose that X has nine copies of number 8, and Y has 10 copies of the number 8. Then X-Y will have no 8s. The statement X -= Y should have the same effect as the assignment statement X=X-Y.
C++
A bag can contain more than one copy of an item. This bag behavior is different from a set, which can contain only a single copy of any given item. Write (specify, design, and implement) a new container class set, which is similar to a bag, except that a set can contain only one copy of any given item. You will need to change the interface (specifications) a bit. For example, instead of the bags count function, you will need a constant member function such as this:
bool set::contains(const value_type& item) const;
// Postcondition: TRUE if item is in the set. FALSE
// otherwise.
Similarly, you only need one remove function since there can be only one instance of an element in the set.
Analyze the running time of each of the implemented member functions (constructor, insert, remove, contains, size, +=, +, -=, and -).
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started