我需要一个包含 n 个项目的队列,其中插入 (n+1)th 个项目会删除第 0 个 sup> 项目和插入只能在“背面”进行。
boost 或标准库中是否已经存在这样的结构?
最佳答案
您可以使用 boost::circular_buffer
由 std::queue
包裹,像这样:
#include <queue>
#include <boost/circular_buffer.hpp>
typedef std::queue<my_type, boost::circular_buffer<my_type>> my_queue;
const int n = 3;
...
my_queue q(boost::circular_buffer<my_type>(n));
q.push(1);
q.push(2);
q.push(3);
q.push(4); // queue now contains 2,3,4
关于c++ - 有限大小的队列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11053070/