stack和queue容器的操作比较简单,只需要注意他们不提供迭代器,不能随机访问即可;
# include<iostream>
# include<algorithm>
# include<stack>
# include<queue>
using namespace std;
#if 0
栈和队列都没有迭代器,都不能随机存取,删除
栈容器:先进后出 队列:后进先出
#endif
void test1()
{//栈操作
stack<int> s;
int val;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
stack<int> s1 = s;
cout << "stack.size():" << s.size() << endl;
while(!s.empty())
{
val = s.top();
cout << val << " ";
s.pop();
}
cout << endl;
cout << "stack.size():" << s.size() << endl;
}
void test2()
{//队列操作
queue<int> q;
int val,back;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
cout << "queue.size:" << q.size() << endl;
back = q.back();
cout << "队尾元素是:" << back << endl;
while(!q.empty()){
val = q.front();
cout << val << " ";
q.pop();
}
cout << endl;
cout << "queue.size:" << q.size() << endl;
}
int main()
{
//test1();
test2();
return 0;
}
运行结果:
test1:
test2: