How to access and modify an element inside std::queue in C++
Solution 1
You can use std::deque
for this purpose. It is not possible to access randomly with std:queue using subscript operator:
http://www.cplusplus.com/reference/deque/deque/
Solution 2
The std::queue
is a C++ Standard Library container adapter designed to operate as a FIFO abstract data structure, i.e. it doesn't allow you to access elements inside it: it only allows you to push
elements into the beginning and pop
them off the end.
If you want the access to inner elements for reading or modification, you should choose a different data structure. The selection will depend on the operations most often performed with it. If you used std::queue
without explicitly specifying the container (most probably), it used the std::deque
container behind the scenes, so you can just use it directly: it will even allow you to access the elements inside it using indexing, i.e.
std::deque<SomeStruct> data;
data.push_back(x); // instead of queue.push(...);
data.pop_front(); // instead of queue.pop();
data[i]; // to access elements inside the queue. Note that indexing start from the beginning of the structure, i.e. data[0] is the next element to be pop'ed, not the last push'ed.
Related videos on Youtube
user2192519
Updated on June 04, 2022Comments
-
user2192519 almost 2 years
As it says in title, how can I access multiple members of a structure in a queue without getting them out of the queue? I just want to change the value of an
int
in the structure but I need to keep them in queue for later use.-
Pubby almost 11 yearsUse deque and operator[].
-
unkulunkulu almost 11 yearsWhich element exactly do you want to modify? Are you trying to find some element first? Title doesn't make much sense.
-
unkulunkulu almost 11 yearsAnd technically, queue as an abstract data structure doesn't allow for modifications, it's just that: FIFO data structure.
-
user2192519 almost 11 yearsI have a struct that is used to create a queue which will be filled at start.Then I want to access the top elements of the queue (number of elements can and possibly will change over time but is intended to be more than 5 let's say), change one of the item of those elements (an int that has values 0 or 1 or 2) and then proceed to the next element that needs to be changed.When all those are done for the number of elements that's defined the rest of the programm continues.I don't know if it's doable that's why I am asking.
-
Benjamin Lindley almost 11 yearsIf you need to do this, then you've chosen the wrong data structure. By choosing to use a queue, you are specifically stating: "I do not want to be able to access elements in the middle of this container". Now, you're asking, "How do I access elements in the middle of this container which I specifically requested to be denied access to?"
-
user2192519 almost 11 yearsI see.That makes a lot of sense. Unfortunately I'm obliged to use a queue so I will just do something else (maybe I'll try using 2 queues,dequeue elements change them and add them in another queue for the rest of the code.). Thanks anyway.
-
-
user2192519 almost 11 yearsWhat's the difference between
push
andpush_back
andpop
andpop_front()
? -
milleniumbug almost 11 years@user2192519
push()
andpop()
arestd::queue
operations,push_back()
andpop_front()
arestd::deque
operations. Other than that, they are identical. -
user2192519 almost 11 yearsOh I see.Still that doesn't help much.Thanks anyway.I will try to get around the problem using something else since I need to access only a number of the top elements in the queue.