c++ 如何让程序从类中删除元素

r7xajy2e  于 2023-03-14  发布在  其他
关注(0)|答案(2)|浏览(129)

我目前正在学习C++,不知道如何让程序从项目列表中删除数量为0的项目。

class Item {
private:
    std::string name;
    int quantity;
    float price;

public:
    Item(
            std::string name,
            int quantity,
            float price
    ) :
            name{std::move(name)},
            quantity{quantity},
            price{price} {

    }

    std::string get_name() const {
        return name;
    }

    int get_quantity() const {
        return quantity;
    }

    void set_quantity(int new_quantity) {
        quantity = new_quantity;
    }

    float get_price() const {
        return price;
    }

    bool is_match(const std::string &other) {
        return name == other;
    }
};

class Inventory {
private:
    Item *items[20];
    float total_money;
    int item_count;

    static void display_data(Item &item) {
        std::cout << "\nItem name: " << item.get_name();
        std::cout << "\nQuantity: " << item.get_quantity();
        std::cout << "\nPrice: " << item.get_price();
    }

public:
    Inventory() :
            items{},
            total_money{0},
            item_count{0} {

    }

    void sell_item() {
        std::string item_to_check;
        std::cin.ignore();
        std::cout << "\nEnter item name: ";
        std::cin >> item_to_check;

        for (int i = 0; i < item_count; i++) {
            if (items[i]->is_match(item_to_check)) {
                remove_item(i);
                return;
            }
        }
        std::cout << "\nThis item is not in your Inventory";
    }

    void remove_item(int item_index) {
        int input_quantity;
        Item *item = items[item_index];
        std::cout << "\nEnter number of items to sell: ";
        std::cin >> input_quantity;

        int quantity = item->get_quantity();
        if (input_quantity <= quantity) {
            float price = item->get_price();
            float money_earned = price * input_quantity;
            item->set_quantity(quantity - input_quantity);
            std::cout << "\nItems sold";
            std::cout << "\nMoney received: " << money_earned;
            total_money += money_earned;
        } else {
            std::cout << "\nCannot sell more items than you have.";
        }
    }
};

我试过使用javatpoint等网站的基本方法,比如使用循环来删除元素,但是不起作用。有什么建议吗?

k97glaaz

k97glaaz1#

你不能从一个固定的数组中删除一个元素,但是你可以把后面的元素的值移到数组的一个位置,然后递减你的item_count,例如:

void remove_item(int item_index) {
    int input_quantity;
    Item *item = items[item_index];
    std::cout << "\nEnter number of items to sell: ";
    std::cin >> input_quantity;

    int quantity = item->get_quantity();
    if (input_quantity <= quantity) {
        ...
        item->set_quantity(quantity - input_quantity);
        ...
        if (item->get_quantity() <= 0) {
            delete item; // <-- assuming your Items are created with 'new'...
            for(int i = item_index + 1; i < item_count; ++i) {
                Items[i-1] = Items[i];
            }
            --item_count;
            Items[item_count] = nullptr;
        }
    } else {
        std::cout << "\nCannot sell more items than you have.";
    }
}

也就是说,您确实应该使用std::vector而不是固定数组,例如:

#include <vector>
#include <memory>

...

class Inventory {
private:
    std::vector<std::unique_ptr<Item>> items;
    // don't need item_count anymore!
    ...

    ...

public:
    ...

    void sell_item() {
        ...

        for (size_t i = 0; i < items.size(); ++i) {
            if (items[i]->is_match(item_to_check)) {
                remove_item(i);
                return;
            }
        }

        ...
    }

    void remove_item(size_t item_index) {
        int input_quantity;
        auto &item = items[item_index];
        std::cout << "\nEnter number of items to sell: ";
        std::cin >> input_quantity;

        int quantity = item->get_quantity();
        if (input_quantity <= quantity) {
            ...
            item->set_quantity(quantity - input_quantity);
            ...
            if (item->get_quantity() <= 0)
                items.erase(items.begin() + item_index);
        } else {
            std::cout << "\nCannot sell more items than you have.";
        }
    }

    void add_item() {
        ...
        items.push_back(std::make_unique<Item>(name, quantity, price));
    }
};
nxowjjhe

nxowjjhe2#

你可以在从内存中释放项目后使用一个简单的循环和数组移位。由于数组是静态的,这是唯一的方法。在移除相关项目后,所有元素都需要向后移位一个插槽。

void remove_item(int item_index)
{
    int input_quantity;
    Item *item = items[item_index];
    cout << "\nEnter number of items to sell: ";
    cin >> input_quantity;

    int quantity = item->get_quantity();
    if (input_quantity <= quantity)
    {
        float price = item->get_price();
        float money_earned = price * input_quantity;
        item->set_quantity(quantity - input_quantity);
        cout << "\nItems sold";
        cout << "\nMoney received: " << money_earned;
        total_money += money_earned;
    }
    else
    {
        cout << "\nCannot sell more items than you have.";
    }
    if (item->get_quantity() == 0)
    {
        removeItemFromList(item);
    }
}

// remove item from list once quantity is 0
void removeItemFromList(Item *item)
{
    delete item;
    for (int i = 0; i < item_count; i++)
    {
        if (items[i] == item)
        {
            for (int j = i; j < item_count - 1; j++)
            {
                items[j] = items[j + 1];
            }
            item_count--;
            break;
        }
    }
    item = nullptr;
}

相关问题