像数组一样访问 C++ 队列元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5877504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:04:48  来源:igfitidea点击:

Access c++ queue elements like an array

c++stlqueue

提问by neuromancer

Can queue elements be accessed like an array? If not, then what containers similar to a queue can?

队列元素可以像数组一样访问吗?如果不是,那么有哪些类似于队列的容器可以呢?

回答by Doug T.

This is a task ideal for std::deque. Its optimized for adding/removing onto the end but also provides random access to elements in the middle. To quote the linked article:

这是std::deque 的理想任务。它针对在末尾添加/删除进行了优化,但也提供了对中间元素的随机访问。引用链接的文章:

A deque is very much like a vector: like vector, it is a sequence that supports random accessto elements, constant time insertion and removal of elements at the end of the sequence, and linear time insertion and removal of elements in the middle.

... deque also supports constant time insertion and removal of elements at the beginning of the sequence

deque很像vector:和vector一样,它是一个序列,支持元素的随机访问序列末尾元素的恒定时间插入和移除,以及中间元素的线性时间插入和移除。

... deque 还支持在序列的开头插入和删除元素的恒定时间

So because it can efficiently add/remove from both ends, deque can be used efficiently as a queue with its push_back and pop_front methods:

因此,因为它可以有效地从两端添加/删除,所以 deque 可以有效地用作具有 push_back 和 pop_front 方法的队列:

std::deque<int> aDeque;

// enqueue
aDeque.push_back(1);
aDeque.push_back(2);

// dequeue
int top = aDeque.front();
aDeque.pop_front();

Accessing elements like an array means using the subscript operator

像数组一样访问元素意味着使用下标运算符

dequealso support the random access through the subscript operator:

deque还支持通过下标运算符进行随机访问:

std::cout << aDeque[0];

回答by Fred Nurk

Can queue elements be accessed like an array?

队列元素可以像数组一样访问吗?

Sure! As long as the underlying container (which defaults to deque) does, though you might want to call the code bad names...

当然!只要底层容器(默认为 deque)这样做,尽管您可能想调用代码坏名...

template<class T, class C=std::deque<T> >
struct pubqueue : std::queue<T, C> {
  using std::queue<T, C>::c;

  static C& get_c(std::queue<T, C> &s) {
    return s.*&pubqueue::c;
  }
  static C const& get_c(std::queue<T, C> const &s) {
    return s.*&pubqueue::c;
  }
};

template<class T, class C>
C& get_c(std::queue<T, C> &a) {
  return pubqueue<T, C>::get_c(a);
}
template<class T, class C>
C& get_c(std::queue<T, C> const &a) {
  return pubqueue<T, C>::get_c(a);
}

int main() {
  std::queue<int> q;
  q.push(42);
  std::cout << get_c(q)[0] << '\n';

  pubqueue<int> p;
  p.push(3);
  std::cout << p.c[0] << '\n';

  return 0;
}

Notice the trick that allows you to change your std::queue variables to pubqueue variables and just access the container member directly. This lets you keep the push/pop (instead of push_back/pop_front, etc.) interface of std::queue.

请注意允许您将 std::queue 变量更改为 pubqueue 变量并直接访问容器成员的技巧。这使您可以保留 std::queue 的 push/pop(而不是 push_back/pop_front 等)接口。

回答by Brian Kelly

Since you've clarified that you want subscript operator access, the answer is no. Queues are not a data structure that would ever require random element access. If you need random element access, use a vector, or an actual array.

既然您已经阐明您想要下标运算符访问,那么答案是否定的。队列不是一种需要随机元素访问的数据结构。如果您需要随机元素访问,请使用向量或实际数组。

回答by Alan

The answer, it depends on the implementation of the queue. The queue provided by the Standard Template Library, doesn't give you random access to elements via the subscript operator, because the implementation of random access defeats the point of a queue.

答案,取决于队列的实现。标准模板库提供的队列不会让您通过下标运算符随机访问元素,因为随机访问的实现会破坏队列的点。

Recall that a Queue is a datastructure that provides first-in-first-out behavior. This means you need to really concern yourself with the head-element, and thats it. Once you need access to elements beside the head, you no longer have a queue.

回想一下,队列是一种提供先进先出行为的数据结构。这意味着您需要真正关注头部元素,仅此而已。一旦你需要访问头部旁边的元素,你就不再有队列了。

Now that doesn't mean you can't implement your own queue on top of an array/vector class, however it's not going to be efficient, as both arrays and vectors aren't ideal for adding and removing elements dynamically.

现在这并不意味着您不能在数组/向量类之上实现自己的队列,但是它不会高效,因为数组和向量都不是动态添加和删除元素的理想选择。

回答by levis501

Instead of a queue, use a vector. Queue doesn't use the [] operator.

使用向量代替队列。队列不使用 [] 运算符。

回答by Code_So1dier

Out of STL following containers can use operator[] access: vector, dequeue, map, bitset.

在 STL 之外,以下容器可以使用 operator[] 访问:vector、dequeue、map、bitset。

The default container to use is vector container. In your case dequeue is the most valuable option (because you want to have queue operations as well as random access).

要使用的默认容器是矢量容器。在您的情况下,出队是最有价值的选择(因为您想要进行队列操作以及随机访问)。

Have a look at reference sheet that shows available operations on STL containers: http://www.cplusplus.com/reference/stl/

查看显示 STL 容器上可用操作的参考表:http: //www.cplusplus.com/reference/stl/

Diagram to identify what type of container you need to use (at the bottom of the page): http://www.linuxsoftware.co.nz/cppcontainers.html

标识您需要使用的容器类型的图表(在页面底部):http: //www.linuxsoftware.co.nz/cppcontainers.html

回答by Nikos

std::queuehas no random element access, it's a sequence container adaptor by default using std::dequeue. However, it's worthy noting that if you're using microsoft's compiler cl, you can use ._Get_container()method that lets you access the underlying container and thus its individual elements, like so:

std::queue没有随机元素访问,默认情况下它是一个序列容器适配器,使用std::dequeue. 但是,值得注意的是,如果您使用的是 microsoft 的 compiler cl,则可以使用._Get_container()允许您访问底层容器及其各个元素的方法,如下所示:

std::deque<int> dq;

std::queue<int, decltype(dq)> q;
q.push(23);
q.push(90);
q.push(38794);
q.push(7);
q.push(0);
q.push(2);
q.push(13);
q.push(24323);
q.push(0);
q.push(1234);

for (int i = 0; i < q.size(); i++)
{
    std::cout << q._Get_container()[i] << '\n';
}

hth.

嗯。