这俩容器较为简单,都不可遍历容器。
stack只能访问栈顶元素。 stack--栈
queue只能访问队列中首尾元素。 queue--队列
void test01()
{
stack <int> s;
//入栈
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
while (s.empty() == 0) //等于1代表空
{
//仅能使用s.top查看栈顶元素
cout << "栈顶元素为 " << s.top() << endl;
s.pop();
}
cout << "栈的大小为: "<<s.size() << endl;
}
void test02()
{
queue <int >q;
//入队
q.push(10);
q.push(20);
q.push(30);
q.push(40);
q.push(50);
while (q.empty() == 0) //只要队列不为空
{
//仅能访问队首和队尾
cout << "队头元素" << q.front() <<" ";
cout << "队尾元素" << q.back() << endl;
cout << endl;
q.pop();//出列
}
cout << "队列的大小为" << q.size()<<endl;
}