c++ queues

队列是一种容器的适配器,专门设计在一个FIFO上下文(先入先出),其中元件被插入容器的一端,并从其他提取操作。
队列被实现为容器的适配器,其是使用特定容器类的封装对象作为其底层的容器类,提供一组特定的成员函数来访问它的元素。元件被压入所述特定容器的“后面”,并从它的“前”弹出。
底层容器可以是标准的容器类模板或一些其它专门设计的容器类中的一个。这个基础容器应至少支持以下操作:
ms-help://MS.MSDNQTR.v90.chs/dv_vcstdlib/html/315a806f-cc19-4fbb-a6dd-a7cf8185fdca.htm
empty	判断队列是否为空
size	返回队列元素的数量
front	返回队列的第一个元素
back	返回队列最后一个元素
push	返回队列的第一个元素
pop	从队列中移除第一个元素



#include <queue>
#include <vector>
#include <list>
#include <iostream>

//构造函数
void QueueConstructor(void);

//返回队列最后一个元素
void Queue_back(void);

//判断队列是否为空
void Queue_empty(void);

//返回队列的第一个元素
void Queue_front(void);

//从队列中移除第一个元素
void Queue_pop(void);

//将一个元素添加到队列的后面
void Queue_push(void);

//返回队列元素的数量
void Queue_size(void);

int main()
{
	//QueueConstructor();
	//Queue_back();
	//Queue_empty();
	//Queue_front();
	//Queue_pop();
	//Queue_push();
	Queue_size();
	return 0;
}

//构造函数
void QueueConstructor(void)
{

	using namespace std;

	// Declares queue with default deque base container
	queue <char> q1;

	// Explicitly declares a queue with deque base container
	queue <char, deque<char> > q2;

	// These lines don't cause an error, even though they
	// declares a queue with a vector base container
	queue <int, vector<int> > q3;
	q3.push(10);
	// but the following would cause an error because vector has 
	// no pop_front member function
	// q3.pop( );

	// Declares a queue with list base container
	queue <int, list<int> > q4;

	// The second member function copies elements from a container
	list<int> li1;
	li1.push_back(1);
	li1.push_back(2);
	queue <int, list<int> > q5(li1);
	cout << "The element at the front of queue q5 is "
		<< q5.front() << "." << endl;
	cout << "The element at the back of queue q5 is "
		<< q5.back() << "." << endl;

	return;
	/*
	The element at the front of queue q5 is 1.
	The element at the back of queue q5 is 2.
	请按任意键继续. . .
	*/
}

//返回队列最后一个元素
void Queue_back(void)
{
	using namespace std;
	queue <int> q1;

	q1.push(10);
	q1.push(11);

	int& i = q1.back();
	const int& ii = q1.front();

	cout << "The integer at the back of queue q1 is " << i
		<< "." << endl;
	cout << "The integer at the front of queue q1 is " << ii
		<< "." << endl;

	return;

	/*
	The integer at the back of queue q1 is 11.
	The integer at the front of queue q1 is 10.
	请按任意键继续. . .
	*/
}

//判断队列是否为空
void Queue_empty(void)
{
	using namespace std;

	// Declares queues with default deque base container
	queue <int> q1, q2;

	q1.push(1);

	if (q1.empty())
		cout << "The queue q1 is empty." << endl;
	else
		cout << "The queue q1 is not empty." << endl;

	if (q2.empty())
		cout << "The queue q2 is empty." << endl;
	else
		cout << "The queue q2 is not empty." << endl;

	return;
	/*
	The queue q1 is not empty.
	The queue q2 is empty.
	请按任意键继续. . .
	*/
}

//返回队列的第一个元素
void Queue_front(void)
{
	using namespace std;
	queue <int> q1;

	q1.push(10);
	q1.push(20);
	q1.push(30);

	queue <int>::size_type i;
	i = q1.size();
	cout << "The queue length is " << i << "." << endl;

	int& ii = q1.back();
	int& iii = q1.front();

	cout << "The integer at the back of queue q1 is " << ii
		<< "." << endl;
	cout << "The integer at the front of queue q1 is " << iii
		<< "." << endl;

	return;
	/*
	The queue length is 3.
	The integer at the back of queue q1 is 30.
	The integer at the front of queue q1 is 10.
	请按任意键继续. . .
	*/
}

//从队列中移除第一个元素
void Queue_pop(void)
{
	using namespace std;
	queue <int> q1, s2;

	q1.push(10);
	q1.push(20);
	q1.push(30);

	queue <int>::size_type i;
	i = q1.size();
	cout << "The queue length is " << i << "." << endl;

	i = q1.front();
	cout << "The element at the front of the queue is "
		<< i << "." << endl;

	q1.pop();

	i = q1.size();
	cout << "After a pop the queue length is "
		<< i << "." << endl;

	i = q1.front();
	cout << "After a pop, the element at the front of the queue is "
		<< i << "." << endl;

	return;
	/*
	The queue length is 3.
	The element at the front of the queue is 10.
	After a pop the queue length is 2.
	After a pop, the element at the front of the queue is 20.
	请按任意键继续. . .

	*/
}

//将一个元素添加到队列的后面
void Queue_push(void)
{

	using namespace std;
	queue <int> q1;

	q1.push(10);
	q1.push(20);
	q1.push(30);

	queue <int>::size_type i;
	i = q1.size();
	cout << "The queue length is " << i << "." << endl;

	i = q1.front();
	cout << "The element at the front of the queue is "
		<< i << "." << endl;

	return;
	/*
	The queue length is 3.
	The element at the front of the queue is 10.
	请按任意键继续. . .
	*/
}

//返回队列元素的数量
void Queue_size(void)
{
	using namespace std;
	queue <int> q1, q2;
	queue <int>::size_type i;

	q1.push(1);
	i = q1.size();
	cout << "The queue length is " << i << "." << endl;

	q1.push(2);
	i = q1.size();
	cout << "The queue length is now " << i << "." << endl;

	return;
	/*
	The queue length is 1.
	The queue length is now 2.
	请按任意键继续. . .
	*/
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值