全叫走之后空了没有
运行下面这段程序:
#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);
for (int i = 0; i < 4; i++) q.dequeue();
cout << boolalpha << (q.count == 0) << endl;
}
(本题用 g++ -std=c++17 -O0 编译。)
全部评论