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.
-
3Use deque and operator[].Pubby– Pubby2013-04-28 14:46:00 +00:00Commented Apr 28, 2013 at 14:46
-
Which element exactly do you want to modify? Are you trying to find some element first? Title doesn't make much sense.unkulunkulu– unkulunkulu2013-04-28 14:54:08 +00:00Commented Apr 28, 2013 at 14:54
-
And technically, queue as an abstract data structure doesn't allow for modifications, it's just that: FIFO data structure.unkulunkulu– unkulunkulu2013-04-28 15:00:10 +00:00Commented Apr 28, 2013 at 15:00
-
I 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.user2192519– user21925192013-04-28 15:06:50 +00:00Commented Apr 28, 2013 at 15:06
-
1If 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?"Benjamin Lindley– Benjamin Lindley2013-04-28 15:11:28 +00:00Commented Apr 28, 2013 at 15:11
3 Answers
You can use std::deque for this purpose. It is not possible to access randomly with std:queue using subscript operator:
Comments
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.
3 Comments
push and push_back and pop and pop_front() ?push() and pop() are std::queue operations, push_back() and pop_front() are std::deque operations. Other than that, they are identical.