排进四个之后计数是多少

👁️ 1 人浏览 💬 0 人评论 ❤️ 添加收藏

运行下面这段程序:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct CQueue {
    vector<string> data;
    int head = 0;
    int count = 0;

    explicit CQueue(int c) : data(c) {}
    int cap() const { return (int)data.size(); }

    void enqueue(const string& x) {
        data[(head + count) % cap()] = x;
        count++;
    }

    string dequeue() {
        string x = data[head];
        head = (head + 1) % cap();
        count--;
        return x;
    }
};

int where(const CQueue& q, const string& x) {
    for (int i = 0; i < q.cap(); i++) {
        if (q.data[i] == x) return i;
    }
    return -1;
}

int main() {
    CQueue q(4);
    for (const string& x : {"阿岚", "小满", "阿泰", "南风"}) q.enqueue(x);
    cout << q.count << endl;
}

(本题用 g++ -std=c++17 -O0 编译。)

提交你的答案
请登录后提交答案。
去登录
代码编辑器
Ctrl + Enter 运行
本次输入:
输出:

                        
👩‍🏫
AI
💬 题目评论

全部评论