How can I apply the std::random_shuffle algorithm with a std::queue? I tried this:
std::random_shuffle(myQueue.front(), myQueue.back());
And gives errors:
- No match for ‘operator-‘ in ‘__i- __first’
- No match for ‘operator!=’ in ‘__first != __last’
- No match for ‘operator+’ in ‘__first + 1’
- No match for ‘operator++’ in ‘++ __i’
My queue is holding Card classes, which represent poker cards. I can understand that the error comes from the operations which std::random_shuffle is doing with the queue elements So, even when I don’t need a != operator for my Card class, i wrote one and that error is gone.
But what should I do with the rest of the errors? It makes no sense to write operators +, - and ++ for a Card class.
std::queueis not a container; it’s a container adapter. It adapts a sequence container to restrict its interface to simple queue/dequeue operations. In particular, it doesn’t let you (easily) iterate over the underlying container, which is whatstd::random_shuffleneeds to do. If you don’t want those restrictions, don’t usestd::queue; use a sequence container (such asvectorordeque) directly.There are ways to subvert its interface and meddle with the underlying container, but it would be much better to choose an appropriate container to start with.