STL - queue(队列)

Queue简介

queue是队列容器,是一种“先进先出”的容器。

queue是简单地装饰deque容器而成为另外的一种容器。

#include <queue>

queue对象的默认构造
queue采用模板类实现,queue对象的默认构造形式:queue<T> queT;  如:
queue<int> queInt;            //一个存放int的queue容器。
queue<float> queFloat;     //一个存放float的queue容器。
queue<string> queString;     //一个存放string的queue容器。
...
//尖括号内还可以设置指针类型或自定义类型。

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
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;				//赋值
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();		//1
	int iBack = queIntA.back();		//9

	queIntA.front() = 11;			//11
	queIntA.back() = 19;			//19
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();		//5
	}

demo

#include <iostream>
#include <cstdio>
#include <queue>
#include <algorithm>

using namespace std;

void queueInit()
{
	queue<int> q;
	q.push(1);
	q.push(3);
	q.push(5);

	cout << "size of q: " << q.size() << endl;
	// size of q: 3
	cout << "front element: " << q.front() << endl;
	// front element: 1

	while (!q.empty()) {
		cout << q.front() << ' ';
		q.pop();
	}
	// 1 3 5
	cout << endl;
}

class Teacher
{
public:
	int age;
	char name[32];
public:
	void printTeacher()
	{
		cout << "age: " << age << endl;
	}
};

void queueClass()
{
	Teacher t1, t2, t3;
	t1.age = 21;
	t2.age = 22;
	t3.age = 23;

	queue<Teacher> q1;
	q1.push(t1);
	q1.push(t2);
	q1.push(t3);
	while (!q1.empty()) {
		Teacher tmp = q1.front();
		q1.pop();
		tmp.printTeacher();
	}
	cout << endl;
	/*
	age: 21
	age: 22
	age: 23
	*/

	queue<Teacher *> q2;
	q2.push(&t1);
	q2.push(&t2);
	q2.push(&t3);
	while (!q2.empty()) {
		Teacher *tmp = q2.front();
		q2.pop();
		tmp->printTeacher();
	}
	cout << endl;
	/*
	age: 21
	age: 22
	age: 23
	*/

}

int main()
{
	queueInit();
	queueClass();

	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-04 11:09:48

STL - queue(队列)的相关文章

C++ STL queue 队列容器 基本方法

创建队列 queue<int> que; 读取队首元素 que.front(); 读取队尾元素 que.back(); 元素入队 queue.pust(); 元素出队 queue.pop(); 队列大小 queue.size(); 队列是否为空 queue.empty(); 原文地址:https://www.cnblogs.com/izayoi/p/9629488.html

STL中队列(queue)的使用方法

STL 中队列的使用(queue) 基本操作: push(x) 将x压入队列的末端 pop() 弹出队列的第一个元素(队顶元素),注意此函数并不返回任何值 front() 返回第一个元素(队顶元素) back() 返回最后被压入的元素(队尾元素) empty() 当队列为空时,返回true size() 返回队列的长度 使用方法: 头文件: #include <queue> 声明方法: 1.普通声明 queue<int>q; 2.结构体 struct node { int x, y

STL的队列和栈简单使用

STL的队列和栈简单使用 #include <iostream>#include <cstdio>#include <string.h>#include <algorithm>#include <queue>#include <stack>using namespace std;int main(){ queue<int> Q; stack<int> S; int i; for(i=1;i<=10;i++

poj 2243 bfs 利用 结构体中的step成员保存步数 ,STL的队列

//BFS #include <iostream> #include <queue> using namespace std; bool used[8][8]; int move[8][2]={1,2, -1,2, -2,1, -2,-1, -1,-2, 1,-2, 2,-1, 2,1}; struct position { int i,j; int step; position(int a,int b,int c) { i=a; j=b; step=c; } }; int mai

STL之队列的运用

卡片游戏:很好地介绍了队列的特点和应用 桌上有一叠牌,从第一张牌开始从上往下依次编号1~n.当至少还剩两张牌时进行如下操作:把第一张牌扔掉,然后把新的第一张牌放到整叠牌的最后.输入n,输出每次扔掉的牌,以及最后剩下的牌. 样例输入:7 样例输出:1 3 5 7 4 2 6 代码如下: #include<iostream> #include<queue> using namespace std; queue<int> q; //声明队列 int main() { int

queue队列容器

queue队列也是一种线性存储表,元素的插入在表的一端进行,在表的另一端删除,具有先进先出的特点,插入的一端称为队尾,删除的一端称为队首.C++ STL的队列泛化,默认使用双端队列容器deque作为底层架构.元素的出队不返回队首元素,需要调用取队首函数来获取队首元素.队列是一种常用的数据结构,通常以消息队列的形式应用于进程间通信. 创建queue对象 有以下两种方式. (1)    queue() queue<int> q; (2)    queue(const queue&) que

浅谈C++ STL queue 容器

浅谈C++ STL queue 容器 本篇随笔简单介绍一下\(C++STL\)中\(queue\)容器的使用方法和常见的使用技巧.\(queue\)容器是\(C++STL\)的一种比较基本的容器.我们在学习这个容器的时候,不仅要学到这个容器具体的使用方法,更要从中体会\(C++STL\)的概念. queue容器的概念 \(queue\)在英文中是队列的意思.队列是一种基本的数据结构.而\(C++STL\)中的队列就是把这种数据结构模板化了.我们可以在脑中想象买票时人们站的排队队列.我们发现,在一

Stack集合 Queue队列集合 Hashtable哈希表

Stack集合 干草堆集合 栈集合 栈;stack,先进后出,一个一个赋值,一个一个取值,安装顺序来. 属性和方法 实例化 初始化 Stack st = new Stack(); 添加元素 1 个数 2 Console.WriteLine(st.Count); 3 只要使用一次pop方法,就会从最后一个元素开始排除 弹出 4 Console.WriteLine(st.Pop()); 5 Console.WriteLine(st.Count); 6 只想查看不弹出 7 Console.WriteL

python2.0_s12_day9之day8遗留知识(queue队列&amp;生产者消费者模型)

4.线程 1.语法 2.join 3.线程锁之Lock\Rlock\信号量 4.将线程变为守护进程 5.Event事件 * 6.queue队列 * 7.生产者消费者模型 4.6 queue队列 queue非常有用,当信息必须安全的在多个线程之间进行数据交换的时候就应该想到queue 所以,queue它能保证数据被安全的在多个线程之间进行交换,那他就是天生的线程安全. queue有那么几种: class queue.Queue(maxsize=0) # 先入先出 class queue.LifoQ

python threading模块使用 以及python多线程操作的实践(使用Queue队列模块)

今天花了近乎一天的时间研究python关于多线程的问题,查看了大量源码 自己也实践了一个生产消费者模型,所以把一天的收获总结一下. 由于GIL(Global Interpreter Lock)锁的关系,纯的python代码处理一般逻辑的确无法活动性能上的极大提升,但是在处理需要等待外部资源返回或多用户的应用程序中,多线程仍然可以作为一个比较好的工具来进行使用. python提供了两个模块thread和threading 来支持python的多线程操作.通俗的讲一般现在我们只使用threading