C++标准库中的栈和队列
2016-03-13 13:03
405 查看
C++标准库中的栈和队头文件
栈: #include<stack>
队列:#include<queue>
stack:
stacks
are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class
as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from
the "back" of the specific container, which is known as the top of
the stack.
The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall support the following operations:
empty
size
back
push_back
pop_back
queue:
queues are implemented as containers adaptors,
which are classes that use an encapsulated object of a specific container class as its underlying container, providing a
specific set of member functions to access its elements. Elements are pushed into the "back" of
the specific container and popped from its "front".
The underlying container may be one of the standard container class template or some other specifically designed container class. This underlying container shall support at least the following operations:
empty
size
front
back
push_back
pop_front
(以上摘自Cplusplus.com)
定义方式:
栈:stack<int> s;
队列:queue<int> q;
包含函数:
栈:
队列:
存入结构体
可以将结构体存入栈或队列中
比如存入栈:
在引入头文件#include<stack>之后:
typedef struct Str
{
int i;
int j;
}Str;
stack<Str> s;
Str str;
Str.i = 0;
S.j = 0;
s.push(str);
也可将栈中的数据返回直接用结构体接收:
str = s.top();
栈: #include<stack>
队列:#include<queue>
stack:
stacks
are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class
as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from
the "back" of the specific container, which is known as the top of
the stack.
The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall support the following operations:
empty
size
back
push_back
pop_back
queue:
queues are implemented as containers adaptors,
which are classes that use an encapsulated object of a specific container class as its underlying container, providing a
specific set of member functions to access its elements. Elements are pushed into the "back" of
the specific container and popped from its "front".
The underlying container may be one of the standard container class template or some other specifically designed container class. This underlying container shall support at least the following operations:
empty
size
front
back
push_back
pop_front
(以上摘自Cplusplus.com)
定义方式:
栈:stack<int> s;
队列:queue<int> q;
包含函数:
栈:
s.empty() | 如果栈为空返回true,否则返回false |
s.size() | 返回栈中元素的个数 |
s.pop() | 删除栈顶元素但不返回其值 |
s.top() | 返回栈顶的元素,但不删除该元素 |
s.push() | 在栈顶压入新元素 |
q.empty() | 如果队列为空返回true,否则返回false |
q.size() | 返回队列中元素的个数 |
q.pop() | 删除队列首元素但不返回其值 |
q.front() | 返回队首元素的值,但不删除该元素 |
q.push() | 在队尾压入新元素 |
q.back() | 返回队列尾元素的值,但不删除该元素 |
可以将结构体存入栈或队列中
比如存入栈:
在引入头文件#include<stack>之后:
typedef struct Str
{
int i;
int j;
}Str;
stack<Str> s;
Str str;
Str.i = 0;
S.j = 0;
s.push(str);
也可将栈中的数据返回直接用结构体接收:
str = s.top();
相关文章推荐
- Problem C: C语言习题 整数排序
- c++中vector等容器的实现机制
- opencl C++接口: 关于CL_KERNEL_FUNCTION_NAME的一个坑
- C++程序设计之四书五经(为以后打折买书攒着)
- C小项目——电子词典
- C++实现矩阵压缩
- 九度oj题目1123:采药
- 《你必须知道的495个C语言问题》读书笔记之第15-20章:浮点数、风格、杂项
- 2013年第四届蓝桥杯试题(C/C++本科B组)
- C/C++中extern关键字详解
- 约瑟夫环问题递归解法的一点理解
- 快速排序
- c++第一次上机实验——2
- 《More Effective C++》Rule8:了解各种不同意义的new和delete(上)
- Cpp--string常用函数用法总结
- C/C++中的内置基本类型
- POJ 2362
- c++11的使用心得(二)---右值引用
- c语言中逗号运算符和逗号表达式
- C语言写的学生宿舍管理系统