template <class T, class Container = deque<T>> class stack
#include <stack> using namespace std; stack <T> 変数名; stackx; // 空の stack stack x(y); // stack y で初期化
stack<pair<int, double>> s;
s.emplace(5, 3.14);
s.emplace(make_pair(5, 3.14));
// s.push(10, 1.23); // error
s.push(make_pair(10, 1.23));
while (!s.empty()) {
cout << "(" << s.top().first << ", " << s.top().second << ")"<< endl;
s.pop();
}
cout << endl;
= == < <= != > >=
#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
printf("**push**\n");
s.push(1);
print(s);
s.push(3);
print(s);
s.push(2);
print(s);
// pop
printf("**pop**\n");
print(s);
s.pop();
print(s);
s.pop();
print(s);
s.pop();
print(s);
return 0;
}
**push** 先頭の要素: 1 先頭の要素: 3 先頭の要素: 2 **pop** 先頭の要素: 2 先頭の要素: 3 先頭の要素: 1 stack は空です
| 菅沼ホーム | 本文目次 | 演習問題解答例 | 付録目次 | 索引 |