为什么 std::queue 不执行 insert() 而 std::deque 执行?
Why std::queue doesn't implement insert() while std::deque does?
我正在阅读 std::queue
and I was wondering why there is no method to efficiently insert multiple elements with a single operation, while std::deque
offers std::deque::insert
?
std::queue
是一个适配器,打算 限制为 push/pop 接口。即使 std::vector
是底层实现,它也不会公开 insert
。
没有特别的性能原因,只是一种设计方法:如果您感觉您的容器是一个队列,您只是不暴露不需要的接口。
Insert 允许插入到结构中的任意位置。
std::queue
是一个FIFO结构的抽象接口。你只能在最后添加东西。底层结构不一定具有插入任意位置的有效方法(例如考虑 std::vector
)。因此std::queue
没有通用的插入成员函数。
由于一般的插入函数需要迭代器位置参数,提供多重插入是为了方便,这样您就不必跟踪下一个迭代器位置。推回不需要这个,因为不需要迭代器跟踪,一个简单的循环就足够了。
我正在阅读 std::queue
and I was wondering why there is no method to efficiently insert multiple elements with a single operation, while std::deque
offers std::deque::insert
?
std::queue
是一个适配器,打算 限制为 push/pop 接口。即使 std::vector
是底层实现,它也不会公开 insert
。
没有特别的性能原因,只是一种设计方法:如果您感觉您的容器是一个队列,您只是不暴露不需要的接口。
Insert 允许插入到结构中的任意位置。
std::queue
是一个FIFO结构的抽象接口。你只能在最后添加东西。底层结构不一定具有插入任意位置的有效方法(例如考虑 std::vector
)。因此std::queue
没有通用的插入成员函数。
由于一般的插入函数需要迭代器位置参数,提供多重插入是为了方便,这样您就不必跟踪下一个迭代器位置。推回不需要这个,因为不需要迭代器跟踪,一个简单的循环就足够了。