LoginSignup
20
11

More than 5 years have passed since last update.

C++コンテナ std::stack

Last updated at Posted at 2016-02-08

std::stack

C++コンテナのうちの一つ.データを積み重ねるイメージで末尾挿入、末尾削除を行うことができる.

push()関数

push.cpp
stack<int> s;
s.push(0);
s.push(1);

トップに要素を挿入する.

pop()関数

pop.cpp
stack<int> s;
s.push(10);
s.push(20);
cout << s.top() << endl;  //20
s.pop();
cout << s.top() << endl;  //10

トップ要素を削除する.

empty()関数

empty.cpp
stack<int> s;
cout << s.empty() << endl;  //1
s.push(1);
cout << s.empty() << endl;  //0

空なら1,空でないなら0を返す.

size()関数

size.cpp
stack<int> s;
cout << s.size() << endl;  //0
s.push(0);
s.push(1);
cout << s.size() << endl;  //2

格納されている要素の個数を返す.

top()関数

top.cpp
stack<int> s;
s.push(10);
s.push(20);
cout << s.top() << endl;  //20

s.top -= 5;
cout << s.top() << endl;  //15

トップ要素にアクセスする.

サンプルコード

20
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
11