容器stack的函数
2017-11-08 21:50
155 查看
文章源自:http://blog.csdn.net/cmm0401/article/details/71978187?locationNum=9&fps=1
C++ STL标准库——栈stack介绍
C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。
C++ STL栈stack的头文件为: #include <stack>
C++ STL栈stack的成员函数介绍:
操作比较和分配堆栈
empty()堆栈为空,则返回true。
pop()移除栈顶元素。
push()在栈顶增加元素。
size()返回栈中元素数目。
top()返回栈顶元素。
C++ STL栈stack用法代码举例1
[cpp] view
plain copy
#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;
}
c++ stl栈stack用法代码举例2
[cpp] view
plain copy
<strong><span style="color:#FF0000;">// stack::empty </span></strong>
#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;
}
c++
stl栈stack用法代码举例3
[cpp] view
plain copy
<span style="color:#FF0000;"><strong>// stack::push/pop </strong></span>
#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;
}
c++ stl栈stack用法代码举例4
[cpp] view
plain copy
#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;
}
C++ STL标准库——栈stack介绍
C++ Stack(堆栈) 是一个容器类的改编,为程序员提供了堆栈的全部功能,——也就是说实现了一个先进后出(FILO)的数据结构。
C++ STL栈stack的头文件为: #include <stack>
C++ STL栈stack的成员函数介绍:
操作比较和分配堆栈
empty()堆栈为空,则返回true。
pop()移除栈顶元素。
push()在栈顶增加元素。
size()返回栈中元素数目。
top()返回栈顶元素。
C++ STL栈stack用法代码举例1
[cpp] view
plain copy
#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;
}
c++ stl栈stack用法代码举例2
[cpp] view
plain copy
<strong><span style="color:#FF0000;">// stack::empty </span></strong>
#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;
}
c++
stl栈stack用法代码举例3
[cpp] view
plain copy
<span style="color:#FF0000;"><strong>// stack::push/pop </strong></span>
#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;
}
c++ stl栈stack用法代码举例4
[cpp] view
plain copy
#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;
}
相关文章推荐
- c++ STL stack容器成员函数
- C++中将stack等容器类作为函数形参传递
- 20170514_容器stack的函数
- 如何使用 stack?- 每天5分钟玩转 Docker 容器技术(112)
- c++STL容器(map,set,vector,stack,queue)
- 字符串容器中常用函数(总结)
- STL中vector容器begin(),end(),push_back()等相关函数
- uva 12096 The SetStack Computer (map,set,stack容器)
- C++primer--stack处理括号表达式(顺序容器)
- STL之stack&queue容器(笔记)
- [C++ 从入门到放弃-08]C++STL之stack堆栈容器
- C++容器的insert()函数有以下三种用法: 最终*it=val;
- Numpy中stack(),hstack(),vstack()函数用法介绍及实例
- 利用copy函数简单快速输出/保存vector向量容器中的数据
- 容器vector的函数
- C++语法基础--顺序容器(五)--容器适配器-- queue,priority_queue,stack
- Stack容器
- ActivityStackSupervisor.StartActivityUncheckedLocked()函数分析
- vector容器成员函数resize与reserve的区别
- 20170608_STL 中vector 容器的一些常用函数,vector和数组的比较