从向量中删除unique_ptr的问题

时间:2019-06-08 21:39:16

标签: c++ vector unique-ptr erase-remove-idiom

我正在努力从vector中删除unique_ptr。我有一个向量:

agentsVec(std::accumulate(agentsQuantity.begin(), agentsQuantity.end(), 0, 
                           [](int value, const QuantityMap::value_type& p) { return value + p.second; }))

我要填充一定数量的特工。我通过以下方式进行操作:

在类构造函数列表中:

auto agentVecIter = agentsVec.begin();
    std::for_each(agentsQuantity.begin(), agentsQuantity.end(), [this, &agentVecIter](const QuantityMap::value_type& p)
    {
        std::generate_n(agentVecIter, p.second, [this, &p]()
        {
            auto agent = factory.createAgent(p.first);
            changeAgentOnLattice(generatePosition(), agent->getID());
            return agent;
        });
        std::advance(agentVecIter, p.second);
    });

在负责生成代理的功能中:

std::experimental::erase_if(agentsVec, [this, &positionID](auto const& agent) { 
    auto agentID = agent->getID();
    if (positionID == agentID) {
        Utils::addIDToStack(agentID);
        return true;
    }
    return false;
});

agentsQuantity是一个映射,表示某种类型的代理数量。代理工厂返回std :: unique_ptr;

在程序执行过程中,可能需要根据其ID从向量中删除某些Agent(然后将Agent的ID返回到freeID的堆栈中)。

杀死特工:

for (int i = 0; i < latticeSize; i++) {
    for (int j = 0; j < latticeSize; j++) {
        if (lattice->getAgentID(Position(i, j))) {
            Agent* currentAgent = lattice->getAgentInstance(Position(i, j));
            currentAgent->updateHealth();
            if (currentAgent->getAgentType() == Enums::AgentType::Predator && currentAgent->getHealth() <= -10) {
                lattice->killAgent(Position(i, j));
            }
        }
    }
}

为了测试这一点,我添加了5个将被“杀死”的特工。在大约40%的情况下,我得到了:

调试断言失败!向量迭代器不可取消

我在做什么错?

编辑:更多代码需要澄清。代理的格子(包含agentsVec)在环境类中创建。然后,环境进入无限循环并遍历晶格中的所有位置,并检查其中是否存在某些东西。如果是->它会移动代理并更新其运行状况:

环境::健康更新:

const auto originRow = Utils::BoundaryCondition(origin.first, latticeSize);
    const auto originCol = Utils::BoundaryCondition(origin.second, latticeSize);
    const auto destRow = Utils::BoundaryCondition(destination.first, latticeSize);
    const auto destCol = Utils::BoundaryCondition(destination.second, latticeSize);

    if (getAgentID(origin) == static_cast<int>(Enums::AgentType::Field)) {
        Logger::getInstance().Log("lattice", "\tCannot move - there is no moving creature on origin position");
        return false;
    }

    if (getAgentID(destination) != static_cast<int>(Enums::AgentType::Field)) {
        Logger::getInstance().Log("lattice", "\tCannot move - destination position is occupied");
        return false;
    }

    latticeMap[destRow][destCol] = static_cast<int>(getAgentID(origin));
    latticeMap[originRow][originCol] = static_cast<int>(Enums::AgentType::Field);
    Logger::getInstance().Log("lattice", "\tMoved succesfully");

    return true;

Lattice :: Move agent:

Agent* Lattice::getAgentInstance(Position position) {
    int ID = getAgentID(position);
    auto it = std::find_if(std::execution::par, agentsVec.begin(), agentsVec.end(),
        [=](std::unique_ptr<Agent>& agent) { return agent->getID() == ID; });
    if (it != agentsVec.end()) {
        return it->get();
    }
    return nullptr;
}

Agent getter(在环境中用于从Lattice :: agentsVec获取Agent指针)

Enums::AgentType Lattice::checkAgentType(int ID)
{
    auto it = std::find_if(std::execution::par, agentsVec.begin(), agentsVec.end(), 
    [=](std::unique_ptr<Agent>& agent)  { return agent->getID() == ID; });

    if(it != agentsVec.end()) {
        return it->get()->getAgentType();
    }
    return Enums::AgentType::Unknown;
}

和AgentType吸气剂:

class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> cur;
        helper(nums, 0, cur, res);
        return res;
    }
    void helper(vector<int>& nums, int start, vector<int>& cur, vector<vector<int>>& res){
        res.push_back(cur);
        if(start>=nums.size()) return;
        for(int i=start; i<nums.size();i++){
            cur.push_back(nums[i]);
            helper(nums, i+1, cur, res);
            cur.pop_back();
        }
    }
};

0 个答案:

没有答案