stack 介绍
栈是一种容器适配器,特别为后入先出而设计的一种(LIFO ),那种数据被插入,然后再容器末端取出
栈实现了容器适配器,这是用了一个封装了的类作为他的特定容器,提供了一组成员函数去访问他的元素,元素从特定的容器,也就是堆栈的头取出袁术。
这个基础的容器可能是任何标准的容器类,和一些其他特殊设计的模板类,唯一的要求就是要支持一下的操作
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() );
用于构造一个栈适配器对象。
ctnr
Container object
Containeris the second class template parameter (the type of the underlying container for thestack; by default:deque<T>,
seeclass description).
// 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
|
stack::empty
bool empty ( ) const;
判断是否为空。
Return Value
trueif the container size is0,falseotherwise.
// 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 ( );
在栈的顶部移除元素。
// 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
|
void push ( const T& x );
在栈顶添加元素
// 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
|
size_type size ( ) const;
计算栈对象元素个数
// 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;
}
output:
0. size: 0
1. size: 5
2. size: 4
|
Output:
value_type& top ( );
const value_type& top ( ) const;
返回栈顶元素
// 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:
分享到:
相关推荐
心希盼 c++ STL Stack(栈) 包含了用List和Vector来实现的Stack 详细说明请看“心希盼 Stack.doc”
在c++中如何使用STL里的stack,queue,vector,里面有常用操作还有示意
4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter...
4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter...
主要介绍了C++ STL容器stack和queue详解的相关资料,需要的朋友可以参考下
迭代器,utility,iterator,memory,算法,algorithm,numeric,functional,vector,list,deque,set,map,stack,queue逐章介绍
c++stack_和_queue用法,很好的介绍了STL中stack和queue的用法,及其使用方法
本版本与网上其他资源不同之处在于,此版本可进行编辑,搜索,已进行内容识别扫描。可全选,可编辑,可剪切文字。 部分目录如下: 目录 第一篇预备知识 第1 章C++ 编程技术.........................................
STL重要部分,包含了许多数据结构,有vector(动态增加的数组),queue(队列),stack(堆栈)……甚至也包括string,它也可以看做为一种容器,并且适用所有的容器可用的方法。 7:算法(algorithms)部分。STL重要...
4.3 C++ STL的Visual C++编译 50 4.4 C++ STL的体系结构 52 4.4.1 容器(Container) 52 4.4.2 迭代器(Iterator) 53 4.4.3 算法(Algorithm) 53 4.4.4 函数对象(Function Object) 54 4.4.5 适配器(Adapter...
组成的库来说提供了更好的代码重用机会。在C++标准中,STL被组织为下面的几个头文件:、、、、、、、、、、、、<stack>和。文件中主要介绍了前面八个的使用,并且重点介绍了他们的属性和一些成员函数的使用。
C++的STL string vector list stack queue set map 等常用的容器使用
本文档包含了可在C++和C中通用的STL,简洁并全面的包含了常用的,如vector,stack,queue等。
组成的库来说提供了更好的代码重用机会。在C++标准中,STL被组织为下面的几个头文件:、、、、、、、、、、、、<stack>和。文件中主要介绍了前面八个的使用,并且重点介绍了他们的属性和一些成员函数的使用。
STL的代码从广义上讲分为三类:algorithm(算法)、container(容器)和iterator(迭代器),几乎所有的代码都采用了模板类和模版...在C++标准中,STL被组织为下面的13个头文件:、、、、、、、 、、、、<stack>和。
C++思维导图Xmind文件和.png文件: 构造函数与析构函数思维导图Xmind文件和.png文件 拷贝构造思维导图xmind文件...STL适配器——stack && queue STL——list C++——继承 C++——搜索二叉树 C++——AVL树 C++——红黑树
STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称。现然主要出现在C++中,但在被引入C++之前该技术就...在C++标准中,STL被组织为下面的13个头文件:、、、、、、、、、、、<stack>和。
答案
丰富的范例程序 ...Stack---------------------------------------------------136 Queue---------------------------------------------------138 Priority_queue------------------------------------------139