队列
A basic queue:
class Queue {
public:
queue(int n):arr(n){}
void push(int x){
if(full()){
cout<<"queue is full"<<endl;
return;
}
arr[tail] = x;
tail++;
return;
}
void pop(){
if(empty()){
return;
}
head++;
}
bool empty(){
return tail == head;
}
bool full(){
return tail == arr.size();
}
int front(){
return arr[head];
}
int size(){
return tail - head;
}
void output(){
cout<<"queue:";
for(int i = head; i < tail; i++){
cout << arr[i] << " ";
}
cout << end;
return;
}
private:
vector<int> arr;
int head, tail;
};
int main(){
Queue q(5);
string op;
int value;
while(cin >> op){
if(op == "insert") {
cin >> value;
q.push(value);
}else if(op == "front"){
cout << "front is:" << q.front() << endl;
}else if(op == "pop"){
q.pop();
}else if(op == "size"){
cout << "size is:" << q.size() << endl;
}
q.output();
}
return 0;
}
应用场景:
场景一: CPU超线程技术
一个core同时处理两个队列
场景二:线程池的任务队列
用于缓冲
在这里插入图片描述