元素类型C++ 标准模板库STL 队列 queue 使用方法与应用介绍(一)
2013-05-15 19:31
751 查看
发一下牢骚和主题无关:
queue
queue模板类的定义在<queue>头文件中。
与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默以为deque类型。
定义queue对象的示例代码如下:
queue<int>q1;
queue<double>q2;
queue的基本操作有:
入队,如例:q.push(x);将x接到队列的末了。
出队,如例:q.pop();弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
拜访队首元素,如例:q.front(),即最早被压入队列的元素。
拜访队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
拜访队列中的元素个数,如例:q.size()
queue
sareatypeofcontaineradaptor,specificallydesignedtooperateinaFIFOcontext(first-infirst-out),whereelementsareinsertedintooneendofthecontainerandextractedfromtheother.
queue
sareimplementedas
containersadaptors
,whichareclassesthatuseanencapsulatedobjectofaspecificcontainerclassasits
underlyingcontainer
,providingaspecificsetofmemberfunctionstoaccessitselements.Elementsare
pushed
intothe
"back"
ofthespecificcontainerand
popped
fromits
"front"
.
Theunderlyingcontainermaybeoneofthestandardcontainerclasstemplateorsomeotherspecificallydesignedcontainerclass.Theonlyrequirementisthatitsupportsthefollowingoperations:
front()
back()
push_back()
pop_front()
Therefore,thestandardcontainerclasstemplates
deque
and
list
canbeused.Bydefault,ifnocontainerclassisspecifiedforaparticular
queue
class,thestandardcontainerclasstemplate
deque
isused.
IntheirimplementationintheC++StandardTemplateLibrary,queuestaketwotemplateparameters:
Wherethetemplateparametershavethefollowingmeanings:
T:Typeoftheelements.
Container:Typeoftheunderlyingcontainerobjectusedtostoreandaccesstheelements.
Inthereferenceforthequeuememberfunctions,thesesamenamesareassumedforthetemplateparameters.
每日一道理
宽容,是一种坦荡,可以无私无畏,无拘无束,无尘无染。宽容,是一种豁达,是比海洋和天空更为博大的胸襟,是宽广和宽厚的叠加,延续和升华。宽容有度,宽容无价,宽以待人,这是人生处世的基本法则。
#include<iostream>
#include<queue>
#include<string>
usingnamespacestd;
voidtest_empty()
{
queue<int>myqueue;
intsum(0);
for(inti=1;i<=10;i++)myqueue.push(i);
while(!myqueue.empty())
{
sum+=myqueue.front();
myqueue.pop();
}
cout<<"total:"<<sum<<endl;
}//运行结果:total:55
voidtest_pop()
{
queue<int>myqueue;
intmyint;
cout<<"\nPleaseentersomeintegers(enter0toend):\n";
do
{
cin>>myint;
myqueue.push(myint);
}while(myint);
cout<<"myqueuecontains:";
while(!myqueue.empty())
{
cout<<""<<myqueue.front();
myqueue.pop();
}
}
/********
运行结果:
Pleaseentersomeintegers(enter0toend):
512
605
420
517
532
0
myqueuecontains:5126054205175320
********/
voidtest_size()
{
queue<int>myints;
cout<<"0.size:"<<(int)myints.size()<<endl;
for(inti=0;i<5;i++)myints.push(i);
cout<<"1.size:"<<(int)myints.size()<<endl;
myints.pop();
cout<<"2.size:"<<(int)myints.size()<<endl;
}
/****
运行结果:
0.size:0
1.size:5
2.size:4
****/
intmain()
{
test_empty();
cout<<"\n***********************************************\n";
test_size();
cout<<"\n***********************************************\n";
test_pop();
cout<<"\n***********************************************\n";
queue<string>q;
//insertthreeelementsintothequeue
q.push("These");
q.push("are");
q.push("morethan");
//cout<<"numberofelementsinthequeue:"<<q.size()<<endl;
//readandprinttwoelementsfromthequeue
cout<<q.front();
q.pop();
cout<<q.front();
q.pop();
//cout<<"numberofelementsinthequeue:"<<q.size()<<endl;
//inserttwonewelements
q.push("four");
q.push("words!");
//cout<<"\nnumberofelementsinthequeue:"<<q.size()<<endl;
//skiponeelement
q.pop();
//readandprinttwoelements
cout<<q.front();
q.pop();
cout<<q.front()<<endl;
q.pop();
//printnumberofelementsinthequeue
cout<<"numberofelementsinthequeue:"<<q.size()<<endl;
}
/*******
*运行结果:
total:55
***********************************************
0.size:0
1.size:5
2.size:4
***********************************************
Pleaseentersomeintegers(enter0toend):
512
605
420
517
532
0
myqueuecontains:5126054205175320
***********************************************
Thesearefourwords!
numberofelementsinthequeue:0
Processreturned0(0x0)executiontime:33.512s
Pressanykeytocontinue.
********/
文章结束给大家分享下程序员的一些笑话语录:开发时间
项目经理:如果我再给你一个人,那可以什么时候可以完工?程序员:3个月吧!项目经理:那给两个呢?程序员:1个月吧!
项目经理:那100呢?程序员:1年吧!
项目经理:那10000呢?程序员:那我将永远无法完成任务.
queue
queue模板类的定义在<queue>头文件中。
与stack模板类很相似,queue模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默以为deque类型。
定义queue对象的示例代码如下:
queue<int>q1;
queue<double>q2;
queue的基本操作有:
入队,如例:q.push(x);将x接到队列的末了。
出队,如例:q.pop();弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
拜访队首元素,如例:q.front(),即最早被压入队列的元素。
拜访队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
拜访队列中的元素个数,如例:q.size()
std::queue
FIFOqueuequeue
sareatypeofcontaineradaptor,specificallydesignedtooperateinaFIFOcontext(first-infirst-out),whereelementsareinsertedintooneendofthecontainerandextractedfromtheother.
queue
sareimplementedas
containersadaptors
,whichareclassesthatuseanencapsulatedobjectofaspecificcontainerclassasits
underlyingcontainer
,providingaspecificsetofmemberfunctionstoaccessitselements.Elementsare
pushed
intothe
"back"
ofthespecificcontainerand
popped
fromits
"front"
.
Theunderlyingcontainermaybeoneofthestandardcontainerclasstemplateorsomeotherspecificallydesignedcontainerclass.Theonlyrequirementisthatitsupportsthefollowingoperations:
front()
back()
push_back()
pop_front()
Therefore,thestandardcontainerclasstemplates
and
canbeused.Bydefault,ifnocontainerclassisspecifiedforaparticular
queue
class,thestandardcontainerclasstemplate
isused.
IntheirimplementationintheC++StandardTemplateLibrary,queuestaketwotemplateparameters:
template<classT,classContainer=deque<T>>classqueue; |
T:Typeoftheelements.
Container:Typeoftheunderlyingcontainerobjectusedtostoreandaccesstheelements.
Inthereferenceforthequeuememberfunctions,thesesamenamesareassumedforthetemplateparameters.
每日一道理
宽容,是一种坦荡,可以无私无畏,无拘无束,无尘无染。宽容,是一种豁达,是比海洋和天空更为博大的胸襟,是宽广和宽厚的叠加,延续和升华。宽容有度,宽容无价,宽以待人,这是人生处世的基本法则。
#include<queue>
#include<string>
usingnamespacestd;
voidtest_empty()
{
queue<int>myqueue;
intsum(0);
for(inti=1;i<=10;i++)myqueue.push(i);
while(!myqueue.empty())
{
sum+=myqueue.front();
myqueue.pop();
}
cout<<"total:"<<sum<<endl;
}//运行结果:total:55
voidtest_pop()
{
queue<int>myqueue;
intmyint;
cout<<"\nPleaseentersomeintegers(enter0toend):\n";
do
{
cin>>myint;
myqueue.push(myint);
}while(myint);
cout<<"myqueuecontains:";
while(!myqueue.empty())
{
cout<<""<<myqueue.front();
myqueue.pop();
}
}
/********
运行结果:
Pleaseentersomeintegers(enter0toend):
512
605
420
517
532
0
myqueuecontains:5126054205175320
********/
voidtest_size()
{
queue<int>myints;
cout<<"0.size:"<<(int)myints.size()<<endl;
for(inti=0;i<5;i++)myints.push(i);
cout<<"1.size:"<<(int)myints.size()<<endl;
myints.pop();
cout<<"2.size:"<<(int)myints.size()<<endl;
}
/****
运行结果:
0.size:0
1.size:5
2.size:4
****/
intmain()
{
test_empty();
cout<<"\n***********************************************\n";
test_size();
cout<<"\n***********************************************\n";
test_pop();
cout<<"\n***********************************************\n";
queue<string>q;
//insertthreeelementsintothequeue
q.push("These");
q.push("are");
q.push("morethan");
//cout<<"numberofelementsinthequeue:"<<q.size()<<endl;
//readandprinttwoelementsfromthequeue
cout<<q.front();
q.pop();
cout<<q.front();
q.pop();
//cout<<"numberofelementsinthequeue:"<<q.size()<<endl;
//inserttwonewelements
q.push("four");
q.push("words!");
//cout<<"\nnumberofelementsinthequeue:"<<q.size()<<endl;
//skiponeelement
q.pop();
//readandprinttwoelements
cout<<q.front();
q.pop();
cout<<q.front()<<endl;
q.pop();
//printnumberofelementsinthequeue
cout<<"numberofelementsinthequeue:"<<q.size()<<endl;
}
/*******
*运行结果:
total:55
***********************************************
0.size:0
1.size:5
2.size:4
***********************************************
Pleaseentersomeintegers(enter0toend):
512
605
420
517
532
0
myqueuecontains:5126054205175320
***********************************************
Thesearefourwords!
numberofelementsinthequeue:0
Processreturned0(0x0)executiontime:33.512s
Pressanykeytocontinue.
********/
文章结束给大家分享下程序员的一些笑话语录:开发时间
项目经理:如果我再给你一个人,那可以什么时候可以完工?程序员:3个月吧!项目经理:那给两个呢?程序员:1个月吧!
项目经理:那100呢?程序员:1年吧!
项目经理:那10000呢?程序员:那我将永远无法完成任务.
相关文章推荐
- C++ 标准模板库STL 优先级队列 priority_queue 使用方法与应用介绍(一)
- C++ 标准模板库STL 队列 queue 使用方法与应用介绍(一)
- C++ 标准模板库STL 队列 queue 使用方法与应用介绍(一)
- C++ 标准模板库STL 双端队列 deque 使用方法与应用介绍(一)
- C++ 标准模板库STL multimap 使用方法与应用介绍
- C++ 标准模板库STL multimap 使用方法与应用介绍(一)
- C++ 标准模板库STL 双向链表 list 使用方法与应用介绍(一)
- C++ 标准模板库STL set 使用方法与应用介绍(一)
- C++ 标准模板库STL set 使用方法与应用介绍(二)
- C++ 标准模板库STL 双向链表 list 使用方法与应用介绍(一)
- C++ 标准模板库STL set 使用方法与应用介绍(三)
- C++ STL--stack/queue 的使用方法
- C++ STL--stack/queue 的使用方法
- C++STL map介绍与使用方法
- 【STL】c++ priority_queue的使用方法
- STL中队列(queue)的使用方法
- 优先队列priority_queue容器的使用方法:出队,入队,删除队首元素,判断是否为空
- C++ STL容器的使用方法(vector、queue、list、set、map)
- 转载——C++ STL--stack/queue 的使用方法
- STL——stack/queue 的使用方法 C++