#include <algorithm>

//  Queue - 队列 - 开始
template<typename Data>
class Queue {
    int head, tail, MAXN;
    Data *data;
public:
    Queue(int size = 10000) {
        clear();
        data = new Data[MAXN = size];
    }
    Queue& operator = (Queue &q) {
        std::swap(head, q.head);
        std::swap(tail, q.tail);
        std::swap(MAXN, q.MAXN);
        std::swap(data, q.data);
        return *this;
    }
    void clear() {
        head = tail = 0;
    }
    int size() {
        return tail - head;
    }
    void push(Data a) {
        if(tail >= MAXN) throw MAXN;
        data[tail++] = a;
    }
    Data &shift() {
        return data[head++];
    }
    void replace(Data a, bool (*update)(Data &, Data)) {
        for(int i=head; i<tail; i++)
            if(update(data[i], a)) return;
        push(a);
    }
    void sort(bool (*cmp)(Data, Data)) {
        std::sort(&data[head], &data[tail], cmp);
    }
    ~Queue() {
        delete[] data;
    }
};
//  Queue - 队列 - 结束

一͛世͛珍͛藏͛
86 声望6 粉丝