有一个函数queue::empty()可用于检查队列是否为空–如果队列为空,则返回1(真),否则返回0(假)。
但是,在此示例中–我们通过使用queue::size()函数进行检查。
如果我们不想使用queue::empty()函数,我们可以检查队列的大小,如果它是0 –队列是一个空队列,如果它不是0(大于零),则队列是空的。
程序:
#include <iostream>
#include <queue>
using namespace std;
//主要功能
int main() {
//声明两个队列
queue<int> Q1;
queue<int> Q2;
//向Q1插入元素
Q1.push(10);
Q1.push(20);
Q1.push(30);
if(Q1.size()==0)
cout<<"Q1 is an empty queue"<<endl;
else
cout<<"Q1 is not an empty queue"<<endl;
if(Q2.size()==0)
cout<<"Q2 is an empty queue"<<endl;
else
cout<<"Q2 is not an empty queue"<<endl;
return 0;
}输出结果
Q1 is not an empty queue Q2 is an empty queue