STL--queue学习笔记

本文深入探讨了C++标准模板库(STL)中的queue容器适配器,讲解了其初始化、成员函数及基本操作。并通过实例展示了如何进行入队、出队、访问队首队尾元素等操作,同时提供了清空队列的多种方法。

STL--queue学习笔记

<queue>

只能访问queue<T>容器适配器的第一个和最后一个元素。只能在容器的末尾添加新元素,只能从头部移除元素。FIFO(先进先出)

1.初始化

需要头文件<queue>

queue<int>que;

 

2.成员函数

C++队列Queue类成员函数如下:

back()返回最后一个元素

empty()如果队列空则返回真

front()返回第一个元素

pop()删除第一个元素

push()在末尾加入一个元素

size()返回队列中元素的个数

 

3.queue 的基本操作举例如下:

queue入队,如例:q.push(x); 将x 接到队列的末端。

queue出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。

访问queue队首元素,如例:q.front(),即最早被压入队列的元素。

访问queue队尾元素,如例:q.back(),即最后被压入队列的元素。

判断queue队列空,如例:q.empty(),当队列空时,返回true。

访问队列中的元素个数,如例:q.size()

 

queue队列中没有clear()操作:

因此清空队列有几种方法:

 

第一种:直接用空的队列对象赋值

queue<int>q1
q1=queue<int>();

第二种:遍历出队列

while(!q.empty())q.pop();

第三种:使用swap,这种是最高效的,定义clear,保持STL容器的标准

void clear(queue<int>& q)
{
    queue<int>empty;
    swap(empty,q);
}

 

举个例子

#include<queue>
#include<iostream>
using namespace std;
void clear(queue<int>&q)
{
    queue<int>empty;
    swap(empty,q);
 } 
int main()
{
    queue<int>q;
    q.push(1);                //在队列末尾依次插入1 2 3 
    q.push(2);
    q.push(3);
    
    int u=q.back();            //返回队列中最后一个元素 
    cout<<"队列最后一个元素为:"<<u<<endl;
    
    int v=q.front();        //返回队列中第一个元素 
    cout<<"队列第一个元素为:"<<v<<endl;
    
    q.pop();                //删除第一个元素 
    v=q.front();
    cout<<"队列第一个元素为:"<<v<<endl; 

    int size=q.size();                //size返回元素个数 
    cout<<"队列中存在"<<size<<"个元素"<<endl; 
    
    cout<<"判断队列是否为空,空输出1 否则输出1:"<<endl;
    int flag=q.empty();            //判断队列是否为空,为空返回1,否则返回0 
    cout<<flag<<endl;
//情况queue的三种方法    
/*    q=queue<int>();*/

/*    while(!q.empty())
        q.pop();*/
    
    clear(q);                //queue中没有clear操作,用函数定义clear函数,使用swap 
    cout<<q.empty()<<endl;
    return 0;
 }

 

我的abaqus出现报错 Integration and section point output variables will not be output for deformable elements that are declared as rigid using the *rigid body option Output request peeqvavg is not available for element type c3d8r Output request pevavg is not available for element type c3d8r Output request svavg is not available for element type c3d8r Integration and section point output variables will not be output for deformable elements that are declared as rigid using the *rigid body option Output request peeqvavg is not available for element type c3d8r Output request pevavg is not available for element type c3d8r Output request svavg is not available for element type c3d8r There are 8072 warning messages in the data (.dat) file. Please check the data file for possible errors in the input file. ASSEMBLY_S_SURF-1 is a rigid surface, so it will be made the main surface in the contact pair with surface ASSEMBLY_M_SURF-1. The WEIGHT value reported in the .dat file for this contact pair is not correct. ASSEMBLY_S_SURF-1 is a rigid surface, so it will be made the main surface in the contact pair with surface ASSEMBLY_M_SURF-1. The WEIGHT value reported in the .dat file for this contact pair is not correct. ASSEMBLY_S_SURF-1 is a rigid surface, so it will be made the main surface in the contact pair with surface ASSEMBLY_M_SURF-1. The WEIGHT value reported in the .dat file for this contact pair is not correct. Boundary conditions are defined at the nodes contained in node set WarnNodeBcIntersectKinCon. In addition the nodes are also part of a surface involved in kinematic contact. The kinematic contact constraint will be overridden by the boundary conditions in case of a conflict. Penalty contact may be used instead.
08-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值