You can write your own predicate and use remove_if . A predicate can be a functor that always stores the score previous Student . Something like that:
class ScoreLessThanPrevious { public: ScoreLessThanPrevious() : isFirst(true), previousScore(0) {} bool operator()(const Student & s) { if (isFirst) { isFirst = false; return false; } else { boolean retval = s.score < previousScore; previousScore = s.score; return retval; } } private: bool isFirst; int previousScore; };
As Neil points out, this is not possible with std::queue . However, it will work with sequences like deque , list , set or vector (all that have begin() and end() ).
If you want to do this using queue , do the following:
- Remove the first item from the queue (using
pop ). - Compare the score with the new first item in the queue (access the first item with
front ). - If the score is greater, insert the element again on the back (using
push ), otherwise discard it. - Again from 1. until you see the first item on the front panel again.
To make sure that you are not processing any element twice, you can do this in a loop that takes into account the initial size of the queue.
source share