c++ - 从双端队列中删除某个位置的对象

标签 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,但我还不太了解这些,所以你能帮我吗,谢谢。

最佳答案

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

deque 支持随机访问迭代器,它很像指针(事实上,指针是随机访问迭代器的一种),所以你可以只获取开始迭代器并向其添加一个整数获取偏移量,就像使用指针一样。

关于c++ - 从双端队列中删除某个位置的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11717006/

相关文章:

java - 使用循环数组实现双端队列?

c++ - 负数或超大 STL 双端队列?

C++ 列表函数返回列表

c++ - C++/Qt (Windows) 报告生成器

c++ - 在 QTCreator 中使用 MinGW 编译 c++ 和 cuda 代码

java - 迭代器是如何在 Java 中实现的?

c++ - 图的链表实现

c++ - 调整自定义迭代器以便(a?)reverse_iterator 可以翻转它的输出

C++ OutputIterator 后递增要求

python - 如何通过索引获取双端队列元素?