Access c++ queue elements like an array

一曲冷凌霜 提交于 2019-12-04 16:33:25

问题


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


回答1:


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:

A deque is very much like a vector: like vector, it is a sequence that supports random access to 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

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:

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

deque also support the random access through the subscript operator:

std::cout << aDeque[0];



回答2:


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...

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.




回答3:


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.




回答4:


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.




回答5:


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




回答6:


Out of STL following containers can use operator[] access: 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/

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




回答7:


std::queue has 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::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.



来源:https://stackoverflow.com/questions/5877504/access-c-queue-elements-like-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!