Share via


stack クラス

基になるコンテナー型に最も新しく追加された要素へのアクセスを制限することにより、機能の制限を提供するテンプレート コンテナーのアダプター クラスです。 stack クラスは、stack 操作のみがコンテナーで実行されることが明確であることが重要な場合に使用されます。

構文

template <class Type, class Container= deque <Type>>
class stack

パラメーター

Type
スタックに格納される要素のデータ型。

Container
スタックを実装するために使用する基になるコンテナーの型。 既定値は、クラス deque<Type> です。

解説

スタック オブジェクトの最初のテンプレート パラメーターで指定するクラス Type の要素は value_type と同義で、2 番目のテンプレート パラメーターで指定する、基になるコンテナー クラス Container 内の要素の型と一致する必要があります。 対象の型のオブジェクトをコピーし、対象の型の変数に値を割り当てられるように、Type は割り当て可能でなければいけません。

スタックに適した基になるコンテナー クラスには、dequelist クラスvector クラス、またはbackpush_backpop_back の各操作をサポートするその他すべてのシーケンス コンテナーがあります。 基になるコンテナー クラスは、コンテナー アダプター内にカプセル化されます。コンテナー アダプターは、限られた一連のシーケンス コンテナーのメンバーの関数のみをパブリック インターフェイスとして公開します。

stack オブジェクトは、クラス Type の要素が等価比較できる場合にのみ等価比較でき、クラス Type の要素が小なり比較できる場合にのみ小なり比較できます。

  • このクラスは stack 、最後の先入れ先出し (LIFO) データ構造をサポートします。 思い描くのに助けとなるのは、積み重ねられた皿です。 要素 (皿) は、積み重ねの一番上からのみ挿入、検査、または削除できます。積み重ねの一番上に相当するのは、基本のコンテナーの末尾にある最後の要素です。 一番上の要素にのみアクセスできる制限があることが、stack クラスを使用する理由です。

  • queue クラスは、先入れ先出し (FIFO) のデータ構造をサポートしています。 思い描くのに助けとなるのは、銀行の窓口で並んでいる人です。 要素 (人々) は、列の一番後ろに追加され、列の一番前から取り除くことができます。 列の一番前と一番後ろの両方を検査できます。 このように一番前と一番後ろの要素にのみアクセスできる制限があることが、queue クラスを使用する理由です。

  • priority_queue クラスは、最も大きな要素が常に先頭の位置になるように、その要素を並べ替えます。 要素の挿入、および先頭の要素の検査と削除をサポートしています。 思い描くのに助けとなるのは、年齢、身長、またはその他の条件によって整列している人です。

メンバー

コンストラクター

名前 説明
stack 空であるか、基本のコンテナー オブジェクトのコピーである stack を構築します。

Typedefs

名前 説明
container_type stack によって適合されるように、基本のコンテナーを提供する型。
size_type stack 内の要素の数を表すことができる符号なし整数型。
value_type stack 内に要素として格納されるオブジェクトの種類を表す型。

関数

名前 説明
empty stack が空かどうかをテストします。
pop stack の先頭から要素を削除します。
push stack の先頭に要素を追加します。
size stack 内の要素数を返します。
top stack の先頭にある要素への参照を返します。

container_type

適合されるように、基本のコンテナーを提供する型。

typedef Container container_type;

解説

この型は、テンプレート パラメーター Containerのシノニムです。 C++ 標準ライブラリのシーケンス コンテナー クラスである vector クラス、list クラス、および既定の deque クラスは、いずれも stack オブジェクトの基本のコンテナーとして使用するための要件を満たしています。 こうした要件を満たすユーザー定義型を使用することもできます。

Container の詳細については、stack クラスのトピックのコメントのセクションをご覧ください。

container_type の宣言や使用の方法の例については、stack::stack の例を参照してください。

empty

stack が空かどうかをテストします。

bool empty() const;

戻り値

stack が空の場合は true、stack が空ではない場合は false

// stack_empty.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   // Declares stacks with default deque base container
   stack <int> s1, s2;

   s1.push( 1 );

   if ( s1.empty( ) )
      cout << "The stack s1 is empty." << endl;
   else
      cout << "The stack s1 is not empty." << endl;

   if ( s2.empty( ) )
      cout << "The stack s2 is empty." << endl;
   else
      cout << "The stack s2 is not empty." << endl;
}
The stack s1 is not empty.
The stack s2 is empty.

pop

stack の先頭から要素を削除します。

void pop();

解説

メンバー関数を適用するには、stack を空にすることはできません。 stack の先頭は最も直近に追加された要素によって占有される位置であり、コンテナーの末尾にある最後の要素になります。

// stack_pop.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1, s2;

   s1.push( 10 );
   s1.push( 20 );
   s1.push( 30 );

   stack <int>::size_type i;
   i = s1.size( );
   cout << "The stack length is " << i << "." << endl;

   i = s1.top( );
   cout << "The element at the top of the stack is "
        << i << "." << endl;

   s1.pop( );

   i = s1.size( );
   cout << "After a pop, the stack length is "
        << i << "." << endl;

   i = s1.top( );
   cout << "After a pop, the element at the top of the stack is "
        << i << "." << endl;
}
The stack length is 3.
The element at the top of the stack is 30.
After a pop, the stack length is 2.
After a pop, the element at the top of the stack is 20.

push

stack の先頭に要素を追加します。

void push(const Type& val);

パラメーター

val
スタックの先頭に追加される要素。

解説

stack の先頭は最も直近に追加された要素によって占有される位置であり、コンテナーの末尾にある最後の要素になります。

// stack_push.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1;

   s1.push( 10 );
   s1.push( 20 );
   s1.push( 30 );

   stack <int>::size_type i;
   i = s1.size( );
   cout << "The stack length is " << i << "." << endl;

   i = s1.top( );
   cout << "The element at the top of the stack is "
        << i << "." << endl;
}
The stack length is 3.
The element at the top of the stack is 30.

size

stack 内の要素数を返します。

size_type size() const;

戻り値

stack の現在の長さ。

// stack_size.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1, s2;
   stack <int>::size_type i;

   s1.push( 1 );
   i = s1.size( );
   cout << "The stack length is " << i << "." << endl;

   s1.push( 2 );
   i = s1.size( );
   cout << "The stack length is now " << i << "." << endl;
}
The stack length is 1.
The stack length is now 2.

size_type

stack 内の要素の数を表すことができる符号なし整数型。

typedef typename Container::size_type size_type;

解説

この型は、stack によって採用された基本コンテナーの size_type のシノニムです。

size_type の宣言や使用の方法の例については、size の例を参照してください。

stack

空であるか、基本のコンテナー クラスのコピーである stack を構築します。

stack();

explicit stack(const container_type& right);

パラメーター

right
構築される container のコピー元となる container。

// stack_stack.cpp
// compile with: /EHsc
#include <stack>
#include <vector>
#include <list>
#include <iostream>

int main( )
{
   using namespace std;

   // Declares stack with default deque base container
   stack <char> dsc1;

   //Explicitly declares a stack with deque base container
   stack <char, deque<char> > dsc2;

   // Declares a stack with vector base containers
   stack <int, vector<int> > vsi1;

   // Declares a stack with list base container
   stack <int, list<int> > lsi;

   // The second member function copies elements from a container
   vector<int> v1;
   v1.push_back( 1 );
   stack <int, vector<int> > vsi2( v1 );
   cout << "The element at the top of stack vsi2 is "
        << vsi2.top( ) << "." << endl;
}
The element at the top of stack vsi2 is 1.

top

stack の先頭にある要素への参照を返します。

reference top();

const_reference top() const;

戻り値

stack の先頭にあるコンテナーの最後の要素への参照。

解説

メンバー関数を適用するには、stack を空にすることはできません。 stack の先頭は最も直近に追加された要素によって占有される位置であり、コンテナーの末尾にある最後の要素になります。

top の戻り値が const_reference に割り当てられる場合、stack オブジェクトを変更できません。 top の戻り値が reference に割り当てられる場合、stack オブジェクトを変更できます。

// stack_top.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   stack <int> s1;

   s1.push( 1 );
   s1.push( 2 );

   int& i = s1.top( );
   const int& ii = s1.top( );

   cout << "The top integer of the stack s1 is "
        << i << "." << endl;
   i--;
   cout << "The next integer down is "<< ii << "." << endl;
}
The top integer of the stack s1 is 2.
The next integer down is 1.

value_type

stack 内に要素として格納されるオブジェクトの種類を表す型。

typedef typename Container::value_type value_type;

解説

この型は、stack によって採用された基本コンテナーの value_type のシノニムです。

// stack_value_type.cpp
// compile with: /EHsc
#include <stack>
#include <iostream>

int main( )
{
   using namespace std;
   // Declares stacks with default deque base container
   stack<int>::value_type AnInt;

   AnInt = 69;
   cout << "The value_type is AnInt = " << AnInt << endl;

   stack<int> s1;
   s1.push( AnInt );
   cout << "The element at the top of the stack is "
        << s1.top( ) << "." << endl;
}
The value_type is AnInt = 69
The element at the top of the stack is 69.

関連項目

C++ 標準ライブラリ内のスレッド セーフ
C++ 標準ライブラリ リファレンス