您的位置:首页 > 产品设计 > UI/UE

STL中stack和queue的用法

2011-06-13 16:19 369 查看
发现在平常的编码中,stack和queue用到的频率都非常高,自己写一个stack和queue又显得麻烦,今天特地研究如何使用STL中的stack和queue。研究的并不输入,只是一些简单用法而已。

下面附上我的stack和queue使用代码

#include<iostream>
#include<stack>
#include<queue>
#include<stdlib.h>//system("pause");需要用到
using namespace std;
int main()
{
stack<int> s;
queue<int> q;
int arry[4]={1,2,3,4};
for(int i=0;i<4;i++)
{
s.push(arry[i]);
q.push(arry[i]);
}
//输出栈中元素
cout<<"栈长度为:"<<s.size()<<endl;
for(int i=0;i<4;i++)
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
//输出队列中元素
cout<<"队列长度为:"<<q.size()<<endl;
for(int i=0;i<4;i++)
{
cout<<q.front()<<" ";
q.pop();
}
cout<<endl;
/*int i;
cin>>i;*/
system("pause");
return 0;
}


ps:

在VS2010中按CTRL+F5的时候命令行一闪而过,并没有以前“请按任意键继续. . .”这样的提示了。

在网上查询,最后得出的解决方法是加入stdlib.h的头文件,如下所示:

#include<stdlib.h>//system("pause");需要用到


然后在程序最后加上system("pause");,如果有return的话要写在return之前。如下所示:

system("pause");
return 0;


还有一种解决命令行一闪而没 方法,请参考

http://hi.baidu.com/liuguoyuhot/blog/item/f58f64edffae4a2826979108.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: