2

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.

6
  • 3
    Use deque and operator[]. Commented 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. Commented 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. Commented 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. Commented Apr 28, 2013 at 15:06
  • 1
    If 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?" Commented Apr 28, 2013 at 15:11

3 Answers 3

3

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/

Sign up to request clarification or add additional context in comments.

Comments

1

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

What's the difference between push and push_back and pop and pop_front() ?
@user2192519 push() and pop() are std::queue operations, push_back() and pop_front() are std::deque operations. Other than that, they are identical.
Oh 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.
0

You can use iterator, serarch the element you need to modify, retrive the iterator and cnange the value. Use std::deque for this kind of operations.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.