从双端队列中删除和对象

时间:2012-07-30 07:24:35

标签: c++ iterator deque erase

我有这段代码:

 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;

}

现在我需要删除“ding,ding”行中找到的对象,但我不知道怎么做,我知道迭代器使用得很好我实际上必须从deque_wait中删除这个元组并把它移到deque_run,但我还没有真正理解那些,所以你能帮助我,谢谢。

1 个答案:

答案 0 :(得分:4)

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

deque支持随机访问迭代器,它非常类似于指针(事实上,指针是一种随机访问迭代器),因此您可以只获取开始迭代器并为其添加一个整数来获取偏移,就像你可以使用指针一样。