C++容器——queue

C++ STL容器之queue C++ queue 容器 定义于头文件 <queue>,申明为: template< class T, class Container = std::deque<T> > class queue; std::queue 类是容器适配器,它给予程序员队列的功能——尤其是 FIFO (先进先出)数据结构。 类模板表现为底层容器的包装器——只提供特定... 阅读详情

目录

queue容器

特点:

queue容器的使用

加入头文件

queue容器的定义及初始化

queue容器的使用

添加/减少元素 

访问元素

获取/判断容器


queue容器

是C++标准模板库(STL)中的一种容器,它是一个先进先出的队列。

特点:

元素遵循先进先出的原则:队首元素是最早插入的元素,队尾元素是最后插入的元素,不能在中间位置插入或删除元素。。


queue容器的使用

  1. 加入头文件<queue>
  2. queue容器的定义及初始化
  3. queue容器的用法 

加入头文件

#include<queue>

queue容器的定义及初始化

#include<iostream>
#include<queue>
int main()
{
	std::queue<int> first ({ 1,2,3,4,5 });
	std::queue<int> secend = first;
	std::queue<int> third(first);
	return 0;
}

queue容器的使用

使用和

添加/减少元素 

push():在队列的后面添加一个元素。

pop():删除队列的第一个元素。

emplace():在队列的末尾构造一个新元素,而不是将已构造的元素传递给push() 方法。

                这样可以避免额外的构造和拷贝操作,提高效率。

访问元素

front():访问队列的第一个元素(不删除)。

back():访问队列的最后一个元素(不删除)。

获取/判断容器

 size()获取容器

empty()判断容器

举例子实现:

#include<iostream>
#include<queue>
int main()
{
	std::queue<int>first({1,2});
	first.push(3);
	first.emplace(4);

	std::cout << first.front()<<std::endl;
	std::cout << first.back() << std::endl;

	first.pop();
	std::cout << first.back() << std::endl;

	std::cout << first.size() << std::endl;
	
//empty如果容器是空就返回1,如果不为空就返回0
	if (!first.empty())
	{
		std::cout << "容器不为空";
	}
	return 0;
}

//输出的结果是
1
4
4
3
容器不为空

C++ STL队列(Queue)容器详解 它是一种线性数据结构,与栈类似,但是在队列中,元素从后面添加,从前面删除,而在栈中,元素只能从一端添加和删除。empty()函数用于检查队列是否为空,如果队列为空,则返回true,否则返回false。除了以上成员函数,队列类还有一些其他的功能,如可以通过复制一个已存在的队列来创建一个新的队列、将队列转换为数组等。总的来说,队列是一种非常强大的数据结构,在算法实现过程中经常被使用。接下来,让我们添加元素到队列中。在上面的代码中,push()函数用于向队列中添加元素,而pop()函数用于删除队列的首位元素。 阅读详情

相关推荐

c++STL容器的Queue

Queue简介 queue是队列容器,是一种“先进先出”的容器。 queue是简单地装饰deque容器而成为另外的一种容器。 #include queue对象的默认构造 queue采用模板类实现,queue对象的默认构造形式:queue queT; 如: queue queInt; //一个存放int的queue容器。 queue queFloat; //一个存放float的queue容器。 queue queString; //一个存放string的queu

希望我的博客,能帮上你解决学习中工作中所遇到的问题 591

C++之Queue容器

概念: Queue是一种先进先出(First In First Out,FIFO)的数据结构,它有两个出口。

一起coding,一起嗨。 1172

从零到一:Quartus II连接开发板实战,打通绘图、管脚分配、编译与下载全链路

本文详细介绍了使用Quartus II软件连接FPGA开发板并完成首个项目的全流程。从硬件连接到驱动安装,到绘制电路框图、精准分配管脚,再到编译设置与最终下载,手把手指导您打通从设计到硬件实现的完整链路,是FPGA初学者的实战入门指南。

mqtt6iot的博客 723

【C++ 十五】C++ queue容器

本文包含queue基本概念、queue构造函数、queue赋值操作、queue数据存取、queue大小操作。

qq_45138120的博客 2786

TASK02【datawhale组队学习】视觉感知与手眼协调,计算机视觉在机器人中应用,手眼标定算法实现,深度估计与3D重建

参考教程AI硬件与机器人技术教程:https://github.com/datawhalechina/ai-hardware-robotics视觉感知与手眼协调,计算机视觉在机器人中应用,手眼标定算法实现,深度估计与3D重建单目深度估计 (Monocular Depth Estimation)DPT 的全称是 Dense Prediction Transformer。

m0_52024881的博客 1200

微信小程序一键登录和临时code用JWT进行微信用户授权

2、跨域进行调用接口时候,唯一标识UnionID和会话密钥 session_key如果还是用的我自己的,会出现无法调用微信服务器接口,就需要将唯一标识和会话密钥换成前端的,才可以,我自己认为这样很不合理,暂时还没有找到原因。2、能够获取到openId,但是也只能获取到openid,无法获取用户的相关信息,经过查阅官网发现,现在微信那边已经将用户信息加密,需要调用提供的接口进行解密然后获取用户数据,问题原因已经找到解决方法,还未即使更改。1、我这是一台电脑实现的前端调用后端接口,不用考虑跨域的问题。

qq_67653492的博客 5732

C++——queue容器

queue容器,队列

weixin_47207479的博客 862

华为Atlas 300I推理卡驱动安装全攻略:从环境检查到常见问题解决

本文提供了华为Atlas 300I推理卡驱动安装的完整实战指南。从安装前的系统环境检查、内核版本匹配,到详细的二进制.run文件安装步骤、源码编译及高级定制化部署,涵盖了驱动安装的全流程。文章重点解析了安装过程中的关键决策、常见问题排查与解决方案,并针对生产环境的多卡配置、性能调优及安全加固给出了专业建议,旨在帮助用户高效、稳定地完成AI推理加速卡的部署。

swift5iosmith的博客 640

C++容器适配器stack、queue、priority_queue

C++容器适配器stack、queue、priority_queue

Xpccccc的博客 1221

信道估计准则演进:从LS、MMSE到LMMSE的工程权衡

本文深入探讨了信道估计准则的演进历程,从LS、MMSE到LMMSE的工程权衡。通过对比分析三大经典算法的数学本质、工程实现及适用场景,揭示了在计算复杂度、实时性和性能之间的关键取舍。文章结合4G/5G基站项目实战经验,提供了算法选型的三维决策模型和前沿发展建议,为无线通信系统设计提供实用指导。

weixin_29327525的博客 299

避坑!瑞萨RZ/N2L用J-Link下载调试的那些‘坑’(从驱动版本到硬件架构选择)

本文详细解析了瑞萨RZ/N2L开发中使用J-Link调试时常见的兼容性问题与解决方案,包括驱动版本选择、SWD连接优化及e2 studio调试技巧。特别针对硬件架构与驱动版本的匹配问题提供了实用对照表,帮助开发者高效避开调试陷阱,提升开发效率。

weixin_30576827的博客 448

C++容器:stack和queue

队列是一种容器适配器,专门用于在FIFO上下文(先进先出)中操作,其中从容器一端插入元素,另一端提取元素。队列作为容器适配器实现,容器适配器即将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素。元素从队尾入队列,从队头出队列。底层容器可以是标准容器类模板之一,也可以是其他专门设计的容器类。标准容器类deque和list满足了这些要求。默认情况下,如果没有为queue实例化指定容器类,则使用标准容器deque。queue文档。

DDDDDDDRDDR的博客 1027

SAP供应商主数据_采购视图

供应商主数据_采购视图一.视图说明    供应商的采购视图维护的是采购行为和流程相关数据,维护的组织机构是采购组织。供应商如果有采购行为(如订货方),则需要维护采购视图;如果仅为结算方可无需维护。    采购视图包含采购数据、合伙人功能等两个主要页面。各页面的转换可通过界面上的前

heng0757的专栏 1万+

C++知识整理day7容器——stack和queue容器(包括deque容器、priority_queue容器)

我们可以看到,在我们之前学的容器中,例如vector,里面调的都是内存池,而stack和queue里面都是一个容器适配器。Q:什么是适配器?A:适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口。例如:Q:为什么还要有deque这种容器?A:首先,deque是双带你队列,它允许两端都可以插入和删除元素。

qq_59702185的博客 736

QT如何使用CTK插件搭建应用程序框架?

CTK框架,QT框架,插件系统

cs821984831的博客 742

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

C++ STL priority_queue容器详解

priority_queue 容器适配器模拟的也是队列这种存储结构,即使用此容器适配器存储元素只能“从一端进(称为队尾),从另一端出(称为队头)”,且每次只能访问 priority_queue 中位于队头的元素。但是,priority_queue 容器适配器中元素的存和取,遵循的并不是 “First in,First out”(先入先出)原则,而是“First in,Largest out”原则。直白的翻译,指的就是先进队列的元素并不一定先出队列,而是优先级最大的元素最先出队列。

Lemon_C17y的博客 1127

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

C++ STL Queue容器详解

queue 容器 1 queue 基本概念 概念:Queue是一种先进先出(First In First Out,FIFO)的数据结构,它有两个出口。 一个模板容器适配器类,它提供功能的限制,限制对一些基本容器类型的前端和后端元素的访问权限。 可以在后端添加或从前端移除元素,并且可以在队列的任何一端检查元素。 适用于队列的基础容器类包括 deque 和 list 队列容器允许从一端新增元素,从另一端移除元素 队列中只有队头和队尾才可以被外界使用,因此队列不允许有遍历行为 队列中进数据称为 — 入队

frighting_ing的博客 946

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

C++ 特殊容器 —— queue 用法详解

C++ 特殊容器 —— queue 用法详解 写在前面:近期正在学习C++的STL容器,因此在这里做一下日志记录,主要介绍一些容器基本成员函数的用法, 配上实际用例,并不涉及原理。但别人的博客终究是别人的, 最好自己上手操作一下. 写的不好, 请大神手下留情. 下面说的 “运行之后” 表示: 运行上个语句之后的结果. 一行如果说的太长的话, 就得拖动下面的进度条才能看到后面的内容, 非常麻烦 ...

Ember_Sky 618

c++queue容器

一.queue模版类的定义在头文件中。 queue与stack模版非常类似,queue模版也需要定义两个模版参数,一个是元素类型,一个是容器类型,元素类型是必要的,容器类型是可选的,默认为dqueue类型。 定义queue对象的示例代码如下: queueq1; queueq2; queue的基本操作有: 1.入队:如q.push(x):将x元素接到队列的末端;

hjding的博客 348

C++的queue容器

C++的queue容器如何使用

JackerCSDN的博客 478

C++STL容器之queue和priority_queue

queue是C++标准库的一个容器适配器,用于实现先进先出(FIFO)的队列数据结构。它基于其他底层容器(如 deque或list)实现,默认使用deque作为底层容器。queue是一个简单易用的先进先出(FIFO)容器适配器, 它支持在队尾插入元素、在队头删除元素,以及访问队头和队尾元素。底层容器:默认使用deque,但可以指定其他容器(如 list)。受限访问:只能访问队头或队尾元素,不能随机访问中间元素。插入与删除:队尾插入与队头删除的时间复杂度为O(1)。先进先出:元素从队尾插入,队头删除。

qq_55527904的博客 465

C++ STL——Queue容器、priority_queue

一、queue 1、基本概念 Queue是一种容器适配器 C++ 标准库定义了三种类型的容器适配器:stack、queue 和 priority_queue。 每种适配器都限制了一些基础容器类的功能,以便对标准数据结构提供精确控制的接口。 Queue是一种先进先出(First In First Out,FIFO)的数据结构,它有两个出口。一个模板容器适配器类,它提供功能的限制,限制对一些基本容器类型的前端和后端元素的访问权限。 可以在后端添加或从前端移除元素,并且可以在队列的任何一端检查元素。 和

LiuXF93的博客 421

C++ STL Queue容器使用详解

它基于其他底层容器实现队列数据结构,遵循先进先出(FIFO)原则。是标准模板库(STL)中的。

2301_80167761的博客 594

C++ stl容器stack,queue,priority_queue的底层模拟实现

stack,queue,priority_queue的底层模拟实现

2301_79698419的博客 1235

【C++】STL常用容器Queue基础

queue是队列容器,是一种“先进先出”的容器。 queue是简单地装饰deque容器而成为另外的一种容器。 #include <queue>

不分解的单应矩阵 427

C++ STL 容器之queue

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<queue> using namespace std; /*Queue所有元素的进出都必须符合”先进先出”的条件, 只有queue的顶端元素, 才有机会被外界取用。 Queue不提供遍历功能, 也不提供迭代器。*/ void test() { //...

weixin_43903378的博客 160
上一篇: C++容器——map和pair对组
下一篇: C++容器——stack
普通网友
博客等级 码龄3年 935粉丝 87原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值