繁体   English   中英

C++ 创建固定大小的队列

[英]C++ Create fixed size queue

C++ 中,如何创建一个简单的固定大小队列

我在 Java 和 Python 中做过多次,但我正在寻找一种基于 C++ 的方法。

我需要一个只有 2 个元素的简单 FIFO 队列才能使用pushpop实用程序:我已经知道我可以实现自己的类来执行这种限制,但我的问题旨在知道是否存在任何已经可用的解决方案。

或者是否有可能用数组完成相同的任务? 那也行。

您可以从 queue 继承,然后重新实现 push 方法。 这是一个基本的例子。

#include <queue>
#include <deque>
#include <iostream>

template <typename T, int MaxLen, typename Container=std::deque<T>>
class FixedQueue : public std::queue<T, Container> {
public:
    void push(const T& value) {
        if (this->size() == MaxLen) {
           this->c.pop_front();
        }
        std::queue<T, Container>::push(value);
    }
};

int main() {
    FixedQueue<int, 3> q;
    q.push(1);
    q.push(2);
    q.push(3);
    q.push(4);
    q.push(5);
    q.push(6);
    q.push(7);

    while (q.size() > 0)
    {
        std::cout << q.front() << std::endl;
        q.pop();
    }
}

这将打印

$ g++ fixedqueue.cpp -std=c++17 -o fixedqueue && ./fixedqueue
5
6
7

在 C++ 中,如何创建一个简单的固定大小队列

首先,我认为你真正想要的是一个固定容量的 queue ,即队列的大小本身将被限制为队列的容量,但大小可能会有所不同。 例如,如果队列为空,则大小最初可以为零,并在您将元素插入队列时增加到队列的容量。


使用boost::circular_buffer

Boost 库有库Boost.CircularBuffer ,它实现了boost::circular_buffer容器( boost/circular_buffer.hpp )。 该容器适合作为固定容量的 FIFO。

std::vector不同, boost::circular_buffer容量保持不变,无论您在容器中插入多少元素。 也就是说,幕后不会发生重新分配:如果boost::circular_buffer大小达到其容量,则插入新元素只会覆盖现有元素。

您可以在构造时指定boost::circular_buffer的容量:

boost::circular_buffer<int> cb(2);

cb的容量是2 ,它的初始大小为零,因为容器是空的。 它的大小永远不能超过它的容量。 但是,您可以显式更改容器的容量,例如,通过调用circular_buffer::set_capacity()

通过使用push_back()pop_front()成员函数,您可以将boost::circular_buffer用作 FIFO。 例子:

#include <boost/circular_buffer.hpp>
#include <iostream>

void print(const boost::circular_buffer<int>& cb) {
   std::cout << "size: " << cb.size() << ", capacity: " << cb.capacity() << '\n';

   for (auto const& elem: cb)
      std::cout << elem << ' ';
   std::cout << '\n';
}

auto main() -> int {
   // empty: size is zero
   boost::circular_buffer<int> cb(3);
   print(q);

   cb.push_back(0);
   print(cb);

   cb.push_back(1);
   print(cb);

   cb.push_back(2);  
   print(cb);

   // overwrites the oldest element: 0
   cb.push_back(3);
   print(cb);

   // overwrites the oldest element: 1
   cb.push_back(4);
   print(cb);

   cb.pop_front();
   print(cb);

   cb.pop_front();
   print(cb);

   // empty again
   cb.pop_front();
   print(cb);
}

输出:

size: 0, capacity: 3

size: 1, capacity: 3
0
size: 2, capacity: 3
0 1
size: 3, capacity: 3
0 1 2
size: 3, capacity: 3
1 2 3
size: 3, capacity: 3
2 3 4
size: 2, capacity: 3
3 4
size: 1, capacity: 3
4
size: 0, capacity: 3

使用std::queueboost::circular_buffer

std::queue是一个容器适配器,底层容器默认为std::deque 但是,您可以使用boost::circular_buffer作为std::queue的底层容器,因为它实现了front()back()push_back()pop_front()成员函数:

#include <queue>
#include <boost/circular_buffer.hpp>
#include <cassert>

template<typename T>
using FixedCapacityQueue = std::queue<T, boost::circular_buffer<T>>;

auto main() -> int {
   FixedCapacityQueue<int> fixedCapQueue(boost::circular_buffer<int>(3));

   fixedCapQueue.push(0);
   fixedCapQueue.push(1);
   fixedCapQueue.push(2);
   fixedCapQueue.push(3); // overwrites the 0
   assert(fixedCapQueue.front() == 1);

   fixedCapQueue.pop(); // pops the 1
   assert(fixedCapQueue.front() == 2); 
}

使用循环自定义容器。 在线示例在这里: https : //ideone.com/0nEBZa

#include <array>
#include <iostream>
#include <queue>

template <typename T, size_t N = 2>
class CyclicArray {
 public:
  typedef typename std::array<T, N>::value_type value_type;
  typedef typename std::array<T, N>::reference reference;
  typedef typename std::array<T, N>::const_reference const_reference;
  typedef typename std::array<T, N>::size_type size_type;

  ~CyclicArray() {
    while (size())
      pop_front();
  }

  void push_back(const T& v) {
    if (size_ + 1 > N)
      throw;
    new (&array_[(front_ + size_) % N]) T(v);
    ++size_;
  }

  void pop_front() {
    if (size_ < 1)
      throw;
    front().~T();
    ++front_;
    --size_;
    if (front_ >= N)
      front_ = 0;
  }

  const_reference front() const {
    return *reinterpret_cast<const T*>(&array_[front_]);
  }

  reference front() {
    return *reinterpret_cast<T*>(&array_[front_]);
  }

  size_type size() const {
    return size_;
  }

 private:
  size_type front_ = 0;
  size_type size_ = 0;
  std::array<char[sizeof(T)], N> array_;
};

int main() {
    std::queue<int, CyclicArray<int, 2>> queue;
    queue.push(1);
    queue.push(2);
    queue.pop();
    queue.push(3);
    int f = queue.front();
    queue.pop();
    std::cout << f << std::endl;
    f = queue.front();
    queue.pop();
    std::cout << f << std::endl;
    return 0;
}

输出

2
3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM