////////////////////////////////////////
// 2018/05/08 22:37:04
// stack-all stack functions
// the C++ stack is a container adaoter that gives the programer the functionality
// of a stack -- specifically,a FILO(first in, last-out) date structore.
// push.pop,size,top,empty
#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iterator>
using namespace std;
int main(){
vector<int> v1(5), v2(5), v3(5);
iota(v1.begin(),v1.end(),0);
iota(v2.begin(),v2.end(),5);
iota(v3.begin(),v3.end(),10);
stack<vector<int>> s;
s.push(v1);
s.push(v2);
s.push(v3);
cout << "size of stack 's' = " << s.size() << endl;
if (v3.size() != 2){
s.pop();
}
cout << "size of stack 's' = " << s.size() << endl;
vector<int> top = s.top();
cout << "Contents of v2:";
copy(v2.begin(),v2.end(), ostream_iterator<int>(cout, " "));
cout << endl;
while (!s.empty()){
s.pop();
}
cout << "Stack 's' is " << (s.empty() ? "" : "not") << "empty." << endl;
return 0;
}
/*
OUTPUT:
size of stack 's' = 3
size of stack 's' = 2
Contents of v2:5 6 7 8 9
Stack 's' is empty.
*/
stack-all stack functions
最新推荐文章于 2025-10-29 09:10:52 发布
本文通过一个具体的C++示例程序介绍了如何使用STL中的stack容器。演示了如何向栈中压入元素、检查栈的大小、弹出栈顶元素等基本操作。
637

被折叠的 条评论
为什么被折叠?



