title: Problem with removing unique_ptr from vector
body: <p>I am struggling with removing a unique_ptr from vector. I have a vector:</p>
  
  <pre><code>std::vector&lt;std::unique_ptr&lt;Agent&gt;&gt;;
  </code></pre>
  
  <p>which I am filling with certain number of Agents. I  do it in the following way:</p>
  
  <p>In class constructor list:</p>
  
  <pre><code>agentsVec(std::accumulate(agentsQuantity.begin(), agentsQuantity.end(), 0, 
                             [](int value, const QuantityMap::value_type&amp; p) { return value + p.second; }))
  </code></pre>
  
  <p>And in a function responsible for generating agents:</p>
  
  <pre><code>auto agentVecIter = agentsVec.begin();
      std::for_each(agentsQuantity.begin(), agentsQuantity.end(), [this, &amp;agentVecIter](const QuantityMap::value_type&amp; p)
      {
          std::generate_n(agentVecIter, p.second, [this, &amp;p]()
          {
              auto agent = factory.createAgent(p.first);
              changeAgentOnLattice(generatePosition(), agent-&gt;getID());
              return agent;
          });
          std::advance(agentVecIter, p.second);
      });
  </code></pre>
  
  <p>agentsQuantity is a map which denotes number of agents of certain type. Agent factory returns std::unique_ptr;</p>
  
  <p>During the program there can be a need to delete some Agent from vector, based on it's ID (Agent's ID is then returned to freeID's stack).</p>
  
  <p>Kill agent:</p>
  
  <pre><code>std::experimental::erase_if(agentsVec, [this, &amp;positionID](auto const&amp; agent) { 
      auto agentID = agent-&gt;getID();
      if (positionID == agentID) {
          Utils::addIDToStack(agentID);
          return true;
      }
      return false;
  });
  </code></pre>
  
  <p>To test that, I added 5 agents which will be "killed". In about 40% of cases I got:</p>
  
  <p>Debug Assertion Failed! Vector iterator not dereferencable</p>
  
  <p>What am I doing wrong? </p>
  
  <p>EDIT: More code for clarification. Lattice of agents (which contains agentsVec) is created in Environment class. Environment then step into infinite loop and iterates through all positions in lattice and check if something exists there. If yes -> it moves the agent and updates its health:</p>
  
  <p>Environment::Health update:</p>
  
  <pre><code>for (int i = 0; i &lt; latticeSize; i++) {
      for (int j = 0; j &lt; latticeSize; j++) {
          if (lattice-&gt;getAgentID(Position(i, j))) {
              Agent* currentAgent = lattice-&gt;getAgentInstance(Position(i, j));
              currentAgent-&gt;updateHealth();
              if (currentAgent-&gt;getAgentType() == Enums::AgentType::Predator &amp;&amp; currentAgent-&gt;getHealth() &lt;= -10) {
                  lattice-&gt;killAgent(Position(i, j));
              }
          }
      }
  }
  </code></pre>
  
  <p>Lattice::Move agent:</p>
  
  <pre><code>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&lt;int&gt;(Enums::AgentType::Field)) {
          Logger::getInstance().Log("lattice", "\tCannot move - there is no moving creature on origin position");
          return false;
      }
  
      if (getAgentID(destination) != static_cast&lt;int&gt;(Enums::AgentType::Field)) {
          Logger::getInstance().Log("lattice", "\tCannot move - destination position is occupied");
          return false;
      }
  
      latticeMap[destRow][destCol] = static_cast&lt;int&gt;(getAgentID(origin));
      latticeMap[originRow][originCol] = static_cast&lt;int&gt;(Enums::AgentType::Field);
      Logger::getInstance().Log("lattice", "\tMoved succesfully");
  
      return true;
  </code></pre>
  
  <p>Agent getter (used in Environment to get Agent pointer from Lattice::agentsVec)</p>
  
  <pre><code>Agent* Lattice::getAgentInstance(Position position) {
      int ID = getAgentID(position);
      auto it = std::find_if(std::execution::par, agentsVec.begin(), agentsVec.end(),
          [=](std::unique_ptr&lt;Agent&gt;&amp; agent) { return agent-&gt;getID() == ID; });
      if (it != agentsVec.end()) {
          return it-&gt;get();
      }
      return nullptr;
  }
  </code></pre>
  
  <p>And AgentType getter:</p>
  
  <pre><code>Enums::AgentType Lattice::checkAgentType(int ID)
  {
      auto it = std::find_if(std::execution::par, agentsVec.begin(), agentsVec.end(), 
      [=](std::unique_ptr&lt;Agent&gt;&amp; agent)  { return agent-&gt;getID() == ID; });
  
      if(it != agentsVec.end()) {
          return it-&gt;get()-&gt;getAgentType();
      }
      return Enums::AgentType::Unknown;
  }
  </code></pre>