Operaters (-, -=) for bag class

Question1:

Implement operators for - and -= for the bag class from Section 3.1. For two bags x and y, the bag x-y contains all the items of x, with any 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 number 3 (i.e., 7 - 2 copies of the number 3). In the case where y has more copies of an item than x does, the bag x-y will have no copies of that item. For example, suppose that x has nine copies of the 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 x = x-y;

My answer:

在之前的bag类的基础上添加内容:

    void bag::operator -=(const bag& subend){
        // assert(size() - subend.size() >= 0);
        for(size_type i = 0; i < subend.used; i++){
            for(size_type j = 0; j < this->used; j++){
                if(subend.data[i] == this->data[j]){
                    this->erase_one(subend.data[i]);
                    continue;
                }
            }
        }
    }
    
    bag operator -(const bag& b1, const bag& b2){
        bag answer;

        // assert(b1.size() - b2.size() >= 0);
        answer = b1;
        answer -= b2;
        return answer;
    }

main.cpp

#include <iostream>
#include "bag1.h"

using namespace std;

int main(){
    // prepare to test
    main_savitch_3::bag bag1;// 现在是空的
    cout << "Declare a new bag called bag1." << endl;
    cout << "Keep inserting new items into bag1 until it is full." << endl;
    for(int i = 0; i < bag1.CAPACITY; i++){
        bag1.insert(i);
        cout << "Now there are " << bag1.size() << " item(s) in bag1." << endl;
    }
    cout << "Erase one item from bag1." << endl;
    if(bag1.erase_one(28) == true){
        cout << "Succeed to erase 28 from bag1" << endl;
    } else {
        cout << "Failed to erase 28 from bag1" << endl;
    }
    cout << "Now there are " << bag1.size() << " item(s) in bag1." << endl;
    cout << "Declare bag2" << endl;
    main_savitch_3::bag bag2;
    cout << "Insert 2 into bag2" << endl;
    bag2.insert(2);
    cout << "bag2 = bag1 + bag2" << endl;
    bag2 += bag1;
    cout << "How many 2s in bag2?" << endl;
    cout << "Here are " << bag2.count(2) << " 2(s)" << endl;
    cout << "Try to erase 2 from bag2." << endl;
    cout << "Erase " << bag2.erase(2) << " 2(s) from bag2" << endl;
    cout << "How many 2s in bag2 now?" << endl;
    cout << "Here are " << bag2.count(2) << " 2(s)" << endl;
    cout << "How many item(s) in bag2 now?" << endl;
    cout << "Here are " << bag2.size() << " item(s) in bag2 now." << endl;

    cout << "bag3 = bag1 - bag2" << endl;
    main_savitch_3::bag bag3 = bag1 - bag2;
    cout << "The size of bag3: " << bag3.size() << endl;
    return 0;
}

观察bag3 = bag1 - bag2的结果:
bag1
这是bag1的内容,有29个空位被使用,也就是说,从[0]到[28]都是有效的,包含了数字0-27,还有29。
bag2
这是bag2的内容,有28个空位被使用,也就是说,从[0]到[27]都是有效的,包含了数字0、1、3-27,29。
按照规定的 − - 减法运算规则,bag3应当包含数字2。
bag3
确实如此,bag3仅包含数字2。
由于在擦除数字的时候使用的是erase_one函数,故满足被减数小于减数,但结果为0的要求。


  1. Data Structures and Other Objects Using C++ ( Fourth Edition ) Michael Main, Walter Savitch. p179 ↩︎

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Memories off

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值