stl 队列的学习
1.头文件
#include<queue>
2.using namespace std;
是指标识符的各种可见范围。c++标准程序库中的所有标识符都被定于名为std的namespace 中。
(补充点知识:、<iostream>和<iostream.h>格式不一样 前者没有后缀,实际上,在你的编译器include文件夹里面可以看到,二者是两个文件,打开文件就会发现,里面的代码是不一样的。 后缀为.h的头文件c++标准已经明确提出不支持了,早些的实现将标准库功能定义在全局空间里,声明在带.h后缀的头文件里,c++标准为了和C区别开,也为了正确使用命名空间,规定头文件不使用后缀.h。 因 此,当使用<iostream.h>时,相当于在c中调用库函数,使用的是全局命名空间,也就是早期的c++实现;当使用< iostream>的时候,该头文件没有定义全局命名空间,必须使用namespace std;这样才能正确使用cout。)
3.各种操作
队列长度:q.size();
队尾元素:q.back();
判断队列是否为空:q.empty(),为空则返回1
删除队首元素:q.pop();删除队首元素,不能返回队首值
插入元素 q.push();
如for(i=0;i<5;i++) q.push(i);
q.front();返回队首元素
4.定义队列变量
queue<char *>q;(类型为char,变量名为q)
queue<int>q;(类型为int,变量名q
2、结构体
struct node
{
int x, y;
};
queue<node>q;
***************************************************
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
int i;
queue<int>q;
for(i=0;i<5;i++)q.push(i);
cout<<q.size();
puts("");
cout<<q.back();
puts("");
while(!q.empty())
{
cout<<q.front();
puts("");
q.pop();
}
system("pause");
return 0;
}
)
#include<iostream>
#include<queue>
using namespace std;
int main(void)
{
queue<char *>q;
q.push("we");
q.push("want");
q.push("to");
q.push("go");
q.push("to");
q.push("American!");
cout<<"队列的大小为:"<<q.size()<<endl;
cout<<"队列的首元素和尾元素分别是:"<<q.front()<<" "<<q.back()<<endl;
while(!q.empty())
{
cout<<q.front()<<" ";
q.pop();
}
cout<<endl;
system("pause");
return 0;
}
详请请看:http://hi.baidu.com/yuanwenxue/blog/item/805e3ddb2bad116dd0164e94.html
posted on 2011-05-07 17:10 more think, more gains 阅读(384) 评论(0) 收藏 举报
浙公网安备 33010602011771号