1、Queue简介
- queue是队列容器,是一种“先进先出”的容器。
- queue是简单地装饰deque容器而成为另外的一种容器。
-
#include <queue>
2、queue对象的默认构造
queue采用模板类实现,queue对象的默认构造形式:queue<T> queT; 如:
queue<int> queInt;
queue<float> queFloat;
queue<string> queString;
...
3、queue的push()与pop()方法
queue.push(elem);
queue.pop();
queue<int> queInt;
queInt.push(1);queInt.push(3);
queInt.push(5);queInt.push(7);
queInt.push(9);queInt.pop();
queInt.pop();
此时queInt存放的元素是5,7,9
4、queue对象的拷贝构造与赋值
queue(const queue &que);
queue& operator=(const queue &que);
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
queue<int> queIntB(queIntA);
queue<int> queIntC;
queIntC = queIntA;
5、queue的数据存取
queue.back();
queue.front();
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
int iFront = queIntA.front();
int iBack = queIntA.back();
queIntA.front() = 11;
queIntA.back() = 19;
6、queue的大小
queue.empty();
queue.size();
queue<int> queIntA;
queIntA.push(1);
queIntA.push(3);
queIntA.push(5);
queIntA.push(7);
queIntA.push(9);
if (!queIntA.empty())
{
int iSize = queIntA.size();
}
#include <iostream>
using namespace std;
#include <queue>
void main61()
{
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
cout << "队头元素:" << q.front() << endl;
cout << "队列的大小" << q.size() <<endl;
while ( !q.empty())
{
int tmp = q.front();
cout << tmp << " ";
q.pop();
}
}
class Teacher
{
public:
int age;
char name[32];
public:
void printT()
{
cout << "age:" << age << endl;
}
};
void main62()
{
Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
queue<Teacher> q;
q.push(t1);
q.push(t2);
q.push(t3);
while (!q.empty())
{
Teacher tmp = q.front();
tmp.printT();
q.pop();
}
}
void main63()
{
Teacher t1, t2, t3;
t1.age = 31;
t2.age = 32;
t3.age = 33;
queue<Teacher *> q;
q.push(&t1);
q.push(&t2);
q.push(&t3);
while (!q.empty())
{
Teacher *tmp = q.front();
tmp->printT();
q.pop();
}
}
void main()
{
main61();
main62();
main63();
cout<<"hello..."<<endl;
system("pause");
return ;
}