stack
栈容器,数据结构--栈。其特点先进后出,只能访问栈顶元素,不能对容器遍历
#include <iostream>
#include <stack>
using namespace std;
int main()
{
stack<int> s;
for (size_t i = 0; i < 5; i++)
s.push(i+1);//入栈
cout << "s的大小为:" << s.size() << endl;
cout << "s占内存大小:" << sizeof(s) << endl;
for (; !s.empty(); s.pop())//不为空打印并出栈
cout << "栈顶元素为:" << s.top() << endl;
cout << "s的大小为:" << s.size() << endl;
cout << "s占内存大小:" << sizeof(s) << endl;
system("pause");
return 0;
}
queue
队列容器,数据结构--队列。队尾进,对头出,只能访问队头和队尾元素,不能对容器遍历。
#include <iostream>
#include <string>
#include <queue>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->M_name = name;
this->M_age = age;
}
string M_name;
int M_age;
};
int main()
{
queue<Person> q;
Person p[] = { { "唐僧", 30 }, { "熏悟空", 1000 }, { "居八戒", 800 }, { "沙和尚" ,700} };
int length = sizeof(p) / sizeof(p[0]);
cout << length;
for (size_t i = 0; i < length; i++)
q.push(p[i]);//入队
cout << "q的大小为:" << q.size() << endl;
cout << "q占内存大小:" << sizeof(q) << endl;
for (; !q.empty(); q.pop())//不为空打印并出队
{
cout << "队头为:" << q.front().M_name <<"\t年龄:"<<q.front().M_age<<"\t";
cout << "队尾为:" << q.back().M_name << "\t年龄:" << q.back().M_age << endl;;
}
cout << "q的大小为:" << q.size() << endl;
cout << "q占内存大小:" << sizeof(q) << endl;
system("pause");
return 0;
}