从向量中擦除

我有这些课:

class PC
{
public:
    PC(string in_operatingSystem,int in_ramSlots,int in_pcieSlots,int in_totalRamSlots, int in_gbPerRam, int in_cpu, int in_ssd, int in_cost);

    virtual void Print() = 0;
    virtual void Upgrade() = 0;
protected:
    string operatingSystem;
    int ramSlots,pcieSlots,totalRamSlots,gbPerRam;
    int cpu,ssd;
    int cost;
};

class HomePC: public PC
{
public:
    HomePC(string in_operatingSystem,int in_ramSlots,int in_pcieSlots,int in_totalRamSlots, int in_gbPerRam, int in_cpu, int in_ssd, int in_cost, string in_model);

    void Print();
    void Upgrade(){};
private:
    string model;
};

我将内容保存到矢量中,如下所示:

PCList.push_back(new HomePC("MacOS",2,0,1,4,2,256,800,Model));

我试图找到一种方法来在向量中找到特定的模型(例如UserPC)并从列表中删除该PC。

评论
纠缠
纠缠

您可以动态转换每个元素:

for (const auto *pc : PCList) {
    const HomePC *homePc = dynamic_cast<const HomePC *>(pc);
    if (homePc && homePc->getModel() == "UserPC") {
        // erase pc
        break;
    }
}
点赞
评论