简体   繁体   中英

Why std::queue doesn't implement insert() while std::deque does?

我正在阅读std::queue ,我想知道为什么没有方法可以通过单个操作有效地插入多个元素,而std::deque提供了std::deque::insert吗?

Insert allows insertion into an arbitrary position into the structure.

std::queue is an abstract interface for a FIFO structure. You can only add things to the end. The underlying structure doesn't necessarily have an efficient way to insert into arbitrary position (consider std::vector for example). Therefore std::queue has no general insert member function.

Since the general insertion function requires the iterator position argument, the multiple-insert is provided as a convenience, so that you don't have to keep track of the next iterator position. The push back doesn't have need for this because no iterator tracking is needed and a trivial loop is sufficient.

std::queue is an adapter that is intended to be limited to push/pop interfaces. It will not expose insert even if std::vector is an underlying implementation.

There are no particular performance reasons, merely a design approach: if you feel that your container is a queue, you just do not expose interfaces you don't need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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