有限大小的队列

Dip*_*Sen 9 c++ queue boost std

我需要在n个项目上排队,其中(n + 1)项目的插入删除了 0 项目,并且只能在"后面"进行插入.
在boost或标准库中是否已有任何此类结构?

Ste*_*e M 12

你可以使用a 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
Run Code Online (Sandbox Code Playgroud)

  • @DiproSen:`std :: queue`(ever)的唯一目的是防止自己访问底层容器,而不是通过推送一端并读取/弹出另一端.你是那个说你需要一个的人. (6认同)