C++ STL学习之stack
2015-10-19 00:01
531 查看
stack 介绍
栈是一种容器适配器,特别为后入先出而设计的一种(LIFO ),那种数据被插入,然后再容器末端取出
栈实现了容器适配器,这是用了一个封装了的类作为他的特定容器,提供了一组成员函数去访问他的元素,元素从特定的容器,也就是堆栈的头取出袁术。
这个基础的容器可能是任何标准的容器类,和一些其他特殊设计的模板类,唯一的要求就是要支持一下的操作
[cpp] view
plaincopy
•back()
•push_back()
•pop_back()
因此,标准的容器类模板vector, deque 和list可以使用,默认情况下,如果没有容器类被指定成为一个提别的stack
类,标准的容器类模板就是deque 队列。
实现C++ STL,栈有两个参数。
参数示意:
T: 元素类型
Container: 被用于存储和访问元素的的类型
成员函数
stack::stack
explicit stack ( const Container& ctnr = Container() );
用于构造一个栈适配器对象。
ctnrContainer object
Container is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class
description).
[cpp] view
plaincopy
// test_stack.cpp : 定义控制台应用程序的入口点。
//
#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;
}
output:
stack::empty
判断是否为空。
true if the container size is 0, false otherwise.
[cpp] view
plaincopy
// 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;
}
Output:
stack::pop
void pop ( );
在栈的顶部移除元素。
[cpp] view plaincopy// 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;
}
Output:
在栈顶添加元素
[cpp] view plaincopy// 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;
}
Output:
计算栈对象元素个数
Output:
返回栈顶元素
[cpp] view
plaincopy
// test_stack.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stack>
#include <vector>
#include <deque>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
stack<int> mystack;
mystack.push(10);
mystack.push(20);
mystack.top()-=5;
cout << "mystack.top() is now " << mystack.top() << endl;
return 0;
}
Output:
栈是一种容器适配器,特别为后入先出而设计的一种(LIFO ),那种数据被插入,然后再容器末端取出
栈实现了容器适配器,这是用了一个封装了的类作为他的特定容器,提供了一组成员函数去访问他的元素,元素从特定的容器,也就是堆栈的头取出袁术。
这个基础的容器可能是任何标准的容器类,和一些其他特殊设计的模板类,唯一的要求就是要支持一下的操作
[cpp] view
plaincopy
•back()
•push_back()
•pop_back()
因此,标准的容器类模板vector, deque 和list可以使用,默认情况下,如果没有容器类被指定成为一个提别的stack
类,标准的容器类模板就是deque 队列。
实现C++ STL,栈有两个参数。
template < class T, class Container = deque<T> > class stack; |
T: 元素类型
Container: 被用于存储和访问元素的的类型
成员函数
stack::stack
explicit stack ( const Container& ctnr = Container() );
用于构造一个栈适配器对象。
ctnrContainer object
Container is the second class template parameter (the type of the underlying container for thestack; by default: deque<T>, see class
description).
[cpp] view
plaincopy
// test_stack.cpp : 定义控制台应用程序的入口点。
//
#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;
}
output:
size of first: 0 size of second: 3 size of third: 0 size of fourth: 2 |
bool empty ( ) const;
判断是否为空。
Return Value
true if the container size is 0, false otherwise.[cpp] view
plaincopy
// 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;
}
Output:
total: 55 |
void pop ( );
在栈的顶部移除元素。
[cpp] view plaincopy// 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;
}
Output:
Popping out elements... 4 3 2 1 0 |
stack::push
void push ( const T& x );
在栈顶添加元素
[cpp] view plaincopy// 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;
}
Output:
Popping out elements... 4 3 2 1 0 |
stack::size
size_type size ( ) const;
计算栈对象元素个数
[cpp] view plaincopy// stack::size #include <iostream> #include <stack> using namespace std; int main () { stack<int> myints; cout << "0. size: " << (int) myints.size() << endl; for (int i=0; i<5; i++) myints.push(i); cout << "1. size: " << (int) myints.size() << endl; myints.pop(); cout << "2. size: " << (int) myints.size() << endl; return 0; } |
0. size: 0 1. size: 5 2. size: 4 |
stack::top
value_type& top ( ); const value_type& top ( ) const;
返回栈顶元素
[cpp] view
plaincopy
// test_stack.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <stack>
#include <vector>
#include <deque>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
stack<int> mystack;
mystack.push(10);
mystack.push(20);
mystack.top()-=5;
cout << "mystack.top() is now " << mystack.top() << endl;
return 0;
}
Output:
mystack.top() is now 15 |
相关文章推荐
- C++ 动态库&静态库创建和使用
- C语言面试宝典
- 链表
- C/C++常见概念辨析
- 10个数中取最大(初)
- 文章标题
- 100到200之间的素数输出(改)
- STL之hashtable实现
- 交换两个数内容,无临时变量(初)
- 【C++】读取循环数字和cin的利用
- 有N个人顺序排开参加比赛,每个人可以看到自己左右两边比自己得分少的人的分数,最少的人可以得1W的奖金,并且奖金以1w递增,赛委会让大家看到得分比自己少的人得的钱比自己少,编写程序求赛委会至少准备多少钱
- 有参函数
- 诺学网c++视频教程在线观看+打包下载
- 剑指offer第二十三题【二叉搜索树的后序遍历序列】c++实现
- 一般的函数
- C++虚函数表解析(转)
- IOS(C语言学习)第一天
- for循环
- 循环的中断语句
- Combination Sum and Combination Sum II