Search code examples
c++iteratordequeerase

Deleting and object in a position from a deque


I have this code:

 bool tuple_compare(boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> &tuple_from_done)
  {
  for(int i = 0; i < deque_wait.size(); i++) {

      boost::tuple<ppa::Node*, ppa::Node*, ppa::Node*, bool> tuple_from_wait = deque_wait.at(i);
      ppa::Node *father = boost::get<0>(tuple_from_wait);
      ppa::Node *son = boost::get<0>(tuple_from_wait);
      ppa::Node *second_son = boost::get<2>(tuple_from_wait);

      bool has_seq = boost::get<3>(tuple_from_wait);

      cout << "checking this two " << boost::get<1>(tuple_from_wait)->get_name() <<  " bool sequence "
              <<  boost::get<1>(tuple_from_wait)->node_has_sequence_object  << " and this " 
              << boost::get<2>(tuple_from_wait)->get_name() << " bool seq " <<  boost::get<2>(tuple_from_wait)->node_has_sequence_object
              << " with " << boost::get<0>(tuple_from_done)->get_name() << endl;

      if(boost::get<0>(tuple_from_done)->get_name() == boost::get<1>(tuple_from_wait)->get_name()
              || boost::get<0>(tuple_from_done)->get_name() == boost::get<2>(tuple_from_wait)->get_name())
      {
         cout << " found in here this we need to check if there is something if the sons have a sequences!!!! " << endl; 

         if(boost::get<1>(tuple_from_wait)->node_has_sequence_object == true && boost::get<2>(tuple_from_wait)->node_has_sequence_object == true) 
         {
             cout << " ding, ding, we have one ready!!!" << endl;

             return true;
         }
         else
         {
             cout << "not ready yet" << endl;
         }

        }    

       }

  return false;

}

Now I need to delete the object that is found in the line "ding, ding", but I don't know how to do it, I know the iterators are used well I actually have to delete this tuple from the deque_wait and move it to the deque_run, but I don't really understand those yet, so can you help me, thanks.


Solution

  • deque_wait.erase(deque_wait.begin() + i);
    //               ^^^^^^^^^^^^^^^^^^^^^^
    //               that's an iterator
    

    deque supports random access iterators, which are very much like pointers (in fact, pointers are a type of random access iterator), so you can just get the begin iterator and add an integer to it to get the offset, just like you could do with a pointer.