stl::stack容器学习
来源:互联网 发布:oracle 数据库高可用 编辑:程序博客网 时间:2024/05/02 03:09
c++ stl栈stack介绍
C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。
c++ stl栈stack的头文件为:
#include <stack>
c++ stl栈stack的成员函数介绍
操作 比较和分配堆栈
empty() 堆栈为空则返回真
pop() 移除栈顶元素
push() 在栈顶增加元素
size() 返回栈中元素数目
top() 返回栈顶元素
c++ stl栈stack用法代码举例1
// stack::empty
#include <iostream>
#include <stack>
using
namespace
std;
int
main ()
{
stack<
int
> mystack;
int
sum (0);
for
(
int
i=1;i<=10;i++) mystack.push(i);
while
(!mystack.empty())
{
sum += mystack.top();
mystack.pop();
}
cout <<
"total: "
<< sum << endl;
return
0;
}
运行结果
total:55
c++ stl栈stack用法代码举例2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdafx.h"
#include <stack>
#include <vector>
#include <deque>
#include <iostream>
using
namespace
std;
int
_tmain(
int
argc, _TCHAR* argv[])
{
deque<
int
> mydeque(2,100);
vector<
int
> myvector(2,200);
stack<
int
> first;
stack<
int
> second(mydeque);
stack<
int
,vector<
int
> > third;
stack<
int
,vector<
int
> > fourth(myvector);
cout <<
"size of first: "
<< (
int
) first.size() << endl;
cout <<
"size of second: "
<< (
int
) second.size() << endl;
cout <<
"size of third: "
<< (
int
) third.size() << endl;
cout <<
"size of fourth: "
<< (
int
) fourth.size() << endl;
return
0;
}
运行结果
ize of first:0
size of second:2
size of third:0
size of fourth:2
c++ stl栈stack用法代码举例3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// stack::push/pop
#include <iostream>
#include <stack>
using
namespace
std;
int
main ()
{
stack<
int
> mystack;
for
(
int
i=0; i<5; ++i) mystack.push(i);
cout <<
"Popping out elements..."
;
while
(!mystack.empty())
{
cout <<
" "
<< mystack.top();
mystack.pop();
}
cout << endl;
return
0;
}
运行结果
Popping out elements... 4 3 2 1 0
0 0
- stl::stack容器学习
- STL学习(四)stack容器学习
- C++ STL--stack堆栈容器学习笔记
- STL学习笔记之容器--stack
- STL学习笔记-stack容器(栈)
- STL容器适配器:stack
- STL容器之stack
- STL容器之stack
- STL stack容器
- stl之stack容器
- STL容器stack栈
- STL stack堆栈容器
- STL容器之stack
- STL之stack容器
- STL-stack容器
- STL-stack容器
- 标准模板库(STL)学习探究之Stack容器
- STL学习笔记— —特殊容器stack
- 思维 2016.8.13
- cocoa pod升级后出现The dependency `` is not used in any concrete target错误
- 经验分享之在 CentOS 7 上搭建支持 HTTP 协议的 SVN 服务器
- Scala(3)
- 编程学习杂烩
- stl::stack容器学习
- Spring下集成 3.X 的mongo
- 微软C#语言DotNetBar控件的安装和引用问题
- 网络管理常用命令之三 - Netstat 命令详解(图文)
- 算法:从数组中查找重复的元素并分组
- CSS的选择器,优先级
- Android中Service的简单讲解
- 并查集学习入门到熟悉
- 排序算法——快速排序