以下是使用 C++ 实现并进行了明显修改的代码:

#include <iostream>
#include <queue>
using namespace std;

int main() {
    int n;
    cin >> n;

    queue<int> qu;

    while (n--) {
        string str;
        cin >> str;

        if (str == 'push') {
            int x;
            cin >> x;
            qu.push(x);
        }
        else if (str == 'pop') {
            if (!qu.empty())
                qu.pop();
            else
                cout << 'FBI WARNING' << endl;
        }
        else if (str == 'empty') {
            if (qu.empty())
                cout << 'YES' << endl;
            else
                cout << 'NO' << endl;
        }
        else if (str == 'query') {
            if (!qu.empty())
                cout << qu.front() << endl;
            else
                cout << 'FBI WARNING' << endl;
        }
    }

    return 0;
}

在这个修改后的代码中,我使用了 C++ 的 STL 库中的 queue 容器来实现队列操作。通过使用 std::queue,我们可以直接调用 pushpopemptyfront 等函数,使代码更加简洁和易读。

希望这段代码对你有帮助!如果还有其他问题,请随时提问。

C++ 队列操作实现:使用 STL 库提高代码效率

原文地址: https://www.cveoy.top/t/topic/cr2h 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录