template <class T, class Container = deque<T> > class stack
#include <stack> using namespace std; stack <T, Container> 変数名;
== < <= != > >=
#include <stdio.h>
#include <stack>
using namespace std;
void print(stack<int> &q) {
if (q.empty())
printf("stack は空です\n");
else
printf("先頭の要素: %d\n", q.top());
}
int main()
{
stack<int> s;
// push
s.push(1);
print(s);
s.push(3);
print(s);
s.push(2);
print(s);
// pop
s.pop();
print(s);
s.pop();
print(s);
s.pop();
print(s);
return 0;
}
(出力)
先頭の要素: 1
先頭の要素: 3
先頭の要素: 2
先頭の要素: 3
先頭の要素: 1
stack は空です
| ホームページ | 目次 | 演習解答例目次 | 付録目次 | 索引 |