stack容器
先进后出,不允许遍历,只有栈顶元素可以被外界访问到
empty,可以判断是否为空
size,可以返回元素个数
queue
stack与queue代码
#include<iostream>
using namespace std;
#include<queue>
#include<stack>
#include<string>
class person {
public:
person(string name,int age) {
this->m_name = name;
this->m_age = age;
}
string m_name;
int m_age;
};
void test() {
queue<person>q;
person p1("唐僧", 30);
person p2("孙悟空", 100);
person p3("猪八戒", 900);
person p4("沙僧", 30);
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
while (!q.empty()) {
cout << "队头元素--姓名:" << q.front().m_name << "年龄:" << q.front().m_age << endl;
cout << "队尾元素--姓名:" << q.back().m_name << "年龄:" << q.back().m_age << endl;
q.pop();
};
cout << "队列的大小:" << q.size() << endl;
stack<int>s;
s.push(10);
s.push(20);
s.push(30);
s.push(40);
while (!s.empty()) {
cout << "栈顶元素为:" << s.top() << endl;
s.pop();
/*cout << "栈的大小:" << s.size() << endl;*/
}
cout << "栈的大小:" << s.size() << endl;
}
int main() {
test();
system("pause");
return 0;
}