Access c++ queue elements like an array
开发者_C百科Can queue elements be accessed like an array? If not, then what containers similar to a queue can?
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];
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.
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.
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.
Instead of a queue, use a vector. Queue doesn't use the [] operator.
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
You can use vector like queue, example:
std::vector<int> v;
int i=0;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
// "dequeue"
i=v[0]; // get 1
v.erase[ v.begin() ); // remove 1 / "dequeue" 1
i=v[0]; // get 2
v.erase[ v.begin() ); // remove 2 / "dequeue" 2
i=v[0]; // get 3
v.erase[ v.begin() ); // remove 3 / "dequeue" 3
i=v[0]; // get 4
v.erase[ v.begin() ); // remove 4 / "dequeue" 4
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.
精彩评论