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

Adh*_*hya 19 c c++ data-structures

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

请告诉我你的建议来解决这个问题.

Ker*_* SB 11

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

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());"
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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);"
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @ aps2012:排序队列有什么意义?队列的定义是FIFO容器. (4认同)
  • @ aps2012我假设OP需要一个`priority_queue`,如果他需要的话. (3认同)

uml*_*ute 7

排队:

  • 使用std :: set来维护你的一组独特元素
  • 将您能够添加到std :: set的任何元素添加到std :: queue

出队:

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


pmr*_*pmr 6

std::queue是一个容器适配器,并使用相对较少的底层成员Container.您可以轻松实现同时包含自定义容器:一个unordered_mapreference_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;
}
Run Code Online (Sandbox Code Playgroud)

编辑:您将要制作my_container一个合适的容器模型(也许还有分配器),但这是另一个完整的问题.感谢Christian Rau指出错误.