Accessing Struct Data in a Queue

Mar 24, 2013 at 8:51pm
I have a queue of structs. I would like to access one of the two pieces of data at the front of the queue that each struct maintains.

Here is some of my code:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <queue> 
using namespace std;

struct Customer
{
	int time;
	int priority;
}cust;


1
2
queue <Customer> lane1;
queue <Customer> lane2;


What I am asking is how do I get the priority of the struct item at the front (lane1.front()), in this case?
Mar 24, 2013 at 8:58pm
Create a linked list, and access tail pointer as front if you add to head, or head pointer as front if you add to tail.
I don't know if your question meant something different though.
Last edited on Mar 24, 2013 at 8:59pm
Mar 24, 2013 at 9:00pm
There is no way to do it without using a linked list?
Mar 24, 2013 at 9:05pm
A dynamic array should work too.
Mar 24, 2013 at 9:29pm
Im still confused. You mean keep a parallel list to the queue and use that?
Mar 24, 2013 at 10:04pm
Sorry, I did not realize you were already using the std::queue.

To access an element, use
lane1.front()->priority
Mar 24, 2013 at 10:07pm
Ok, that makes sense. But I got this error on that code:

expression must have pointer type
Mar 24, 2013 at 10:11pm
all you did, at least in the above code, is declare the queue. You must add things to it before you can access them.

Oh and if your queue is Customer then use . else Customer* then use ->

lane1[0].priority; //me thinks
Last edited on Mar 24, 2013 at 10:13pm
Mar 24, 2013 at 10:14pm
I think that works. I could have sworn I tried that originally but I mist have spelled something wrong. Thank you. Have a great day!
Topic archived. No new replies allowed.