deque与队列在C ++ [英] deque vs. queue in C++

查看:284
本文介绍了deque与队列在C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Queue和Stack是一个广泛提及的结构。但是,在C ++中,对于队列你可以通过两种方式:

  #include< queue& 
#include< deque>

但是对于堆栈你只能这样做

  #include< stack> 

我的问题是,队列和deque之间有什么区别,对于堆栈,可以包括任何其他结构?

解决方案

Moron是正确的, p>

队列和堆栈是更高级的容器。比deque或列表或向量。



例如:



pre> std :: stack< int,std :: deque< int> > s;
std :: queue< double,std :: list< double> > q;

将使用deque作为底层容器并使用列表创建一个双精度队列作为底层容器。



您可以将s视为受限制的deque,将q视为受限列表。



所有必要的是较低级容器实现较高级容器所需的方法。这些是堆栈的back(),push_back()和pop_back(),队列的front(),back(),push_back()和pop_front()。



堆栈 queue 以获得更多细节。



对于deque,它远不止一个队列,你可以在两端插入。特别地,它具有随机存取运算符[]。这使它更像一个向量,但是一个向量,你可以插入和删除在开头用push_front()和pop_front()。



请参阅deque 了解详情。


Queue and Stack are a structures widely mentioned. However, in C++, for queue you can do it in two ways:

#include <queue>
#include <deque>

but for stack you can only do it like this

#include <stack>

My question is, what's the difference between queue and deque, why two structures proposed? For stack, any other structure could be included?

解决方案

Moron is correct, but a little more detail may be helpful.

queue and stack are higher level containers. than deque or list or vector. By this I mean that you can build a queue or stack out of the lower level containers.

For example:

  std::stack<int, std::deque<int> > s;
  std::queue<double, std::list<double> > q;

Will build a stack of ints using a deque as the underlying container and a queue of doubles using a list as the underlying container.

You can think of s as a restricted deque and q as a restricted list.

All that is necessary is that the lower level container implements the methods needed by the higher level container. These are back(), push_back() and pop_back() for stack and front(), back(), push_back() and pop_front() for queue.

See stack and queue for more detail.

With respect to the deque, it is much more than a queue where you can insert at both ends. In particular, it has the random access operator[]. This makes it more like a vector, but a vector where you can insert and delete at the beginning with push_front() and pop_front().

See deque for detail.

这篇关于deque与队列在C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆