从向量中删除指针

时间:2013-03-11 16:51:12

标签: c++ c++11 std

我使用一个共享指针向量来包含一些名为customer的游戏角色。

typedef std::shared_ptr<Customer> customer;
std::vector<customer> customers;

customers.push_back(customer(new Customer()));

for(int i = 0; i < customers.size(); i++)
{
    if(customers[i]->hasLeftScreen())
    {
        if(!customers[i]->itemRecieved())
            outOfStocks++;
        // Kill Character Here
    }       
}

我之前使用向量来保存对象所以习惯于在向量上调用erase并传入迭代器。我的问题是有一种方法从上面的代码片段中的向量中删除指针?我希望不要在这里使用迭代器来简化代码。我还需要删除指针,因为我是离开屏幕后从游戏中删除的客户。

非常感谢

3 个答案:

答案 0 :(得分:3)

考虑使用迭代器,坦率地说它会更容易处理。我不确定你对它们的反感,但见下文:

std::vector<customer>::iterator it = customers.begin();
while (it != customers.end())
{
    if(it->hasLeftScreen())
    {
        if(!it->itemRecieved())
            outOfStocks++;
        it = customers.erase(it);
        continue;
    }
    ++it;
}

这将从向量中删除共享指针实例。如果实例是对共享指针的 last 引用,它还将释放所述Customer的相关内存,触发其析构函数等...(稍微有点使用智能共享指针的重点) ,顺便说一句,使用智能指针的道具。

答案 1 :(得分:2)

你应该总是使用迭代器;这是一个C ++习语。这会将代码更改为...

for(auto i = customers.begin(); i != customers.end(); ++i)
{
    if((*i)->hasLeftScreen())
    {
        if(!(*i)->itemRecieved())
            outOfStocks++;
        // Kill Character Here
    }       
}

现在很明显,我们使用的是erase-remove idiom

int outOfStocks = 0;
auto it = std::remove_if(customer.begin(), customers.end(), [&](Customer const& i) {
    if(i->hasLeftScreen()) {
        if(!i->itemRecieved()) {
            outOfStocks++;
        }
        return true;
    }
    return false;
}
std::erase(it, customers.end());

答案 2 :(得分:0)

您还可以利用“迭代器算术”:

        // Kill Character Here
        customers.erase(customers.begin() + i);

...但是有一个问题是customers.size()和当前索引将因容器缩小而失效。

此外,您不需要明确delete您要删除的客户,因为智能指针会处理这个问题。

相关问题