STL 栈的使用
2013-11-10 18:41
295 查看
STL 中栈的使用方法(stack)
基本操作:
push(x) 将x加入栈中,即入栈操作
pop() 出栈操作(删除栈顶),只是出栈,没有返回值
top() 返回第一个元素(栈顶元素)
size() 返回栈中的元素个数
empty() 当栈为空时,返回 true
使用方法:
和队列差不多,其中头文件为:
#include <stack>
定义方法为:
stack<int>s1;//入栈元素为 int 型
stack<string>s2;// 入队元素为string型
stack<node>s3;//入队元素为自定义型
/**//*
*===================================*
| |
| STL中优先队列使用方法 |
| |
| chenlie |
| |
| 2010-3-24 |
| |
*===================================*
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int c[100];
struct cmp1
{
bool operator ()(int x, int y)
{
return x > y;//小的优先级高
}
};
struct cmp2
{
bool operator ()(const int x, const int y)
{
return c[x] > c[y];
// c[x]小的优先级高,由于可以在对外改变队内的值,
//所以使用此方法达不到真正的优先。建议用结构体类型。
}
};
struct node
{
int x, y;
friend bool operator < (node a, node b)
{
return a.x > b.x;//结构体中,x小的优先级高
}
};
priority_queue<int>q1;
priority_queue<int, vector<int>, cmp1>q2;
priority_queue<int, vector<int>, cmp2>q3;
priority_queue<node>q4;
queue<int>qq1;
queue<node>qq2;
int main()
{
int i, j, k, m, n;
int x, y;
node a;
while (cin >> n)
{
for (i = 0; i < n; i++)
{
cin >> a.y >> a.x;
q4.push(a);
}
cout << endl;
while (!q4.empty())
{
cout << q4.top().y << " " << q4.top().x << endl;
q4.pop();
}
// cout << endl;
}
return 0;
}
基本操作:
push(x) 将x加入栈中,即入栈操作
pop() 出栈操作(删除栈顶),只是出栈,没有返回值
top() 返回第一个元素(栈顶元素)
size() 返回栈中的元素个数
empty() 当栈为空时,返回 true
使用方法:
和队列差不多,其中头文件为:
#include <stack>
定义方法为:
stack<int>s1;//入栈元素为 int 型
stack<string>s2;// 入队元素为string型
stack<node>s3;//入队元素为自定义型
/**//*
*===================================*
| |
| STL中优先队列使用方法 |
| |
| chenlie |
| |
| 2010-3-24 |
| |
*===================================*
*/
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
int c[100];
struct cmp1
{
bool operator ()(int x, int y)
{
return x > y;//小的优先级高
}
};
struct cmp2
{
bool operator ()(const int x, const int y)
{
return c[x] > c[y];
// c[x]小的优先级高,由于可以在对外改变队内的值,
//所以使用此方法达不到真正的优先。建议用结构体类型。
}
};
struct node
{
int x, y;
friend bool operator < (node a, node b)
{
return a.x > b.x;//结构体中,x小的优先级高
}
};
priority_queue<int>q1;
priority_queue<int, vector<int>, cmp1>q2;
priority_queue<int, vector<int>, cmp2>q3;
priority_queue<node>q4;
queue<int>qq1;
queue<node>qq2;
int main()
{
int i, j, k, m, n;
int x, y;
node a;
while (cin >> n)
{
for (i = 0; i < n; i++)
{
cin >> a.y >> a.x;
q4.push(a);
}
cout << endl;
while (!q4.empty())
{
cout << q4.top().y << " " << q4.top().x << endl;
q4.pop();
}
// cout << endl;
}
return 0;
}
相关文章推荐
- bias和variance
- 11个实用的Apache .htaccess配置
- VS2008 Pocket PC 2003 SE仿真程序上网设置
- STL 队列的使用方法
- android 拍照和上传
- 统计自然语言处理的基础学习之一
- 机器学习&数据挖掘笔记_16(常见面试之机器学习算法思想简单梳理)
- js 获取url的request参数
- Android-ImageView显示选择本地图片或拍照
- jquery ajax
- ubuntu下navicat试用到期解决办法
- 【Java TCP/IP Socket】TCP Socket通信中由read返回值造成的的死锁问题(含代码)
- Android控件之Button图片文字的混排
- Android 调用系统相机拍照保存以及调用系统相册的方法
- Android中从SD卡中/拍照选择图片并进行剪裁的方法
- 解决Python 插查 MySQL 时中文乱码问题
- 到底什么是MVC?
- 爹地,我找到了!15个极好的Linux find命令示例
- 四年多没碰C++了。。。
- [转] 用友NC二次开发问题汇总【转】