[SOLVED] QQueue - Easy way to access tail element?

• I was wondering if there is a function similar to "head()" (that returns the queue head item), but I need the same thing for the "tail".

What I want to do : Average all value inside a Queue, each element of the Queue also representing an average. I need to access the tail element to update it's value with new value that comes in (real-time averaging)

I thought of using array for this, but I would need to move the element all the time, and found QQueue would do this for me (not sure about performance vs array though)

Here is the code I have:

@ // Get current second
int currentSecondPower = (int) timeElapsed_sec;
// If the second changed, add new value to top of queue
if (currentSecondPower != lastSecondPower) {
nbPointsPower = 0;
queuePower.enqueue(value);
// check the queue size, remove element if needed
if (queuePower.size() > settings->averagingPower) {
queuePower.dequeue();
}
}
// If the second is the same, recalculate average and replace value of the tail
else {
double firstEle = queuePower.last() *((double)nbPointsPower/(nbPointsPower+1)); ///how to access tail of queue?
double secondEle = ((double)value)/(nbPointsPower+1);
// replace last element of the queue
queuePower.replace(queuePower.size()-1, firstEle + secondEle);
nbPointsPower++;
}

``    lastSecondPower = currentSecondPower;@``

• A QQueue is just a QList with some bells and whistles.
So you can always use QList::last() (provided your queue is not empty)

• Oh I see, so basically I could use .first() or .last() and it would be like .head() and tail() ?

I guess I could just use a QList and use removeLast() and insert() instead of EnQueue and DeQueue, not sure what is better with a QQueue now..

To replace the last Element of the Queue, i'm using this code, is there a better syntax? :
@ queuePower.replace(queuePower.size()-1, value);@
Thanks

• Hi,

Yes it will.

You could but your code would be less readable. QQueue's name already provides a hint on what is going to happen with that container.

Better syntax ? What do you have in mind ?

• Thanks SGaist I will keep QQueue as you said it implies I want to use it as a Queue.
I thought maybe a cleared way to replace the last element queue.ReplaceLast() but I guess the previous code will do, thanks for your help !