在c ++中使用唯一条目进行队列

时间:2012-07-30 07:23:25

标签: c++ c data-structures

我需要在C或C ++中实现一个包含唯一条目(没有重复项)的队列。我正在考虑维护队列中已有的元素的引用,但这似乎非常低效。

请让我知道你提出的解决这个问题的建议。

4 个答案:

答案 0 :(得分:10)

跟踪唯一性的辅助数据结构如何:

std::queue<Foo> q;
std::set<std::reference_wrapper<Foo>> s;

// to add:

void add(Foo const & x)
{
    if (s.find(x) == s.end())
    {
        q.push_back(x);
        s.insert(std::ref(q.back()));  // or "s.emplace(q.back());"
    }
}

或者,或者,反转队列和集合的角色:

std::set<Foo> s;
std::queue<std::reference_wrapper<Foo>> q;

void add(Foo const & x)
{
    auto p = s.insert(x);       // std::pair<std::set<Foo>::iterator, bool>
    if (s.second)
    {
        q.push_back(std::ref(*s.first));  // or "q.emplace_back(*s.first);"
    }
}

答案 1 :(得分:6)

排队:

  • 使用std :: set维护您的唯一元素集
  • 将您能够添加到std :: set的任何元素添加到std :: queue

出队:

  • 从std :: queue和std :: set
  • 中删除元素

答案 2 :(得分:5)

std::queue是一个容器适配器,使用相对较少的基础Container成员。您可以轻松实现包含unordered_map reference_wrapper<T>deque<T>的自定义容器。它至少需要成员frontpush_back。调用容器的hash_map时检查push_back内部并相应拒绝(可能抛出)。举一个完整的例子:

#include <iostream>
#include <set>
#include <deque>
#include <queue>
#include <unordered_set>
#include <functional>

namespace std {

// partial specialization for reference_wrapper
// is this really necessary?
template<typename T>
class hash<std::reference_wrapper<T>> {
public:
  std::size_t operator()(std::reference_wrapper<T> x) const 
  { return std::hash<T>()(x.get()); }
};

}

template <typename T>
class my_container {
  // important: this really needs to be a deque and only front
  // insertion/deletion is allowed to not get dangling references
  typedef std::deque<T> storage;
  typedef std::reference_wrapper<const T> c_ref_w;
  typedef std::reference_wrapper<T> ref_w;
public:  
  typedef typename storage::value_type value_type;
  typedef typename storage::reference reference; 
  typedef typename storage::const_reference const_reference; 
  typedef typename storage::size_type size_type;

  // no move semantics
  void push_back(const T& t) {
    auto it = lookup_.find(std::cref(t));
    if(it != end(lookup_)) {
      // is already inserted report error
      return;
    }
    store_.push_back(t);
    // this is important to not have dangling references
    lookup_.insert(store_.back());
  }

  // trivial functions

  bool empty() const { return store_.empty(); }
  const T& front() const { return store_.front(); }
  T& front() { return store_.front(); }

  void pop_front() { lookup_.erase(store_.front()); store_.pop_front();  }
private:
  // look-up mechanism
  std::unordered_set<c_ref_w> lookup_;
  // underlying storage
  storage store_;
};

int main()
{
  // reference wrapper for int ends up being silly 
  // but good for larger objects
  std::queue<int, my_container<int>> q;
  q.push(2);
  q.push(3);
  q.push(2);
  q.push(4);
  while(!q.empty()) {
    std::cout << q.front() << std::endl;
    q.pop();
  }

  return 0;
}

编辑:您需要使my_container成为适当的容器模型(也可能是分配器),但这是另一个完整的问题。感谢Christian Rau指出错误。

答案 3 :(得分:3)

您的问题中没有提到一个非常重要的一点,那就是您的项目队列是排序还是具有某种排序(称为排序 a Priority queue)或未排序(称为普通FIFO)。您选择的解决方案仅取决于此问题的答案。

  1. 如果您的队列未排序,那么除了您的队列之外维护额外的数据结构将更有效。使用以某种方式排序以维护队列内容的第二个结构将允许您检查队列中是否已存在项目,或者扫描队列本身的速度是否快得多。添加到未排序队列的末尾需要花费很长时间,并且可以非常有效地完成。

  2. 如果您的队列必须排序,那么将项目放入队列需要您知道队列中项目的位置,这需要队列无论如何要扫描。一旦你知道一个项目的位置,就会知道该项目是否重复,因为如果它是重复的,那么项目将在队列中的该位置已经存在。在这种情况下,所有工作都可以在队列本身上以最佳方式执行,并且不需要维护任何辅助数据结构。

  3. 数据结构的选择取决于您。但是,对于(1)辅助数据结构应该是任何类型的列表或数组,否则扫描辅助索引以扫描原始队列本身将不再有效。