28 STLのStack

4722 ワード

STLのStack
  • STLのStack
  • 概要
  • stackオブジェクトのデフォルト構造
  • stackのpushとpop方法
  • オブジェクトのコピー構造と付与
  • データアクセス
  • stackのサイズ

  • 1.概要
  • stackはスタックコンテナで、「先進後出」のコンテナです.
  • stackはdeque容器を簡単に装飾して別の容器となる.#include

  • 2.stackオブジェクトのデフォルト構造
    stackはテンプレートクラスで実装され、stackオブジェクトのデフォルト構造形式:stack stkT;
    stack <int> stkInt;            //    int stack  。
    stack <float> stkFloat;     //    float stack  。
    stack <string> stkString;     //    string stack  。
    ...                 
    //                   。

    3.stackのpush()とpop()メソッド
    stack.push(elem);   //       
    stack.pop();   //          
    
    stack<int> stkInt;      
    stkInt.push(1);stkInt.push(3);stkInt.pop();   
    stkInt.push(5);stkInt.push(7);  
    stkInt.push(9);stkInt.pop();    
    stkInt.pop();  
    //  stkInt      1,5  

    4.オブジェクトのコピー構造と付与
    stack(const stack &stk);             //      
    stack& operator=(const stack &stk); //       
    stack<int> stkIntA;
    stkIntA.push(1);
    stkIntA.push(3);
    stkIntA.push(5);
    stkIntA.push(7);
    stkIntA.push(9);
    
    stack<int> stkIntB(stkIntA);        //    
    stack<int> stkIntC;

    5.データアクセス
    stack.top();      //           
    stack<int> stkIntA;
    stkIntA.push(1);
    stkIntA.push(3);
    stkIntA.push(5);
    stkIntA.push(7);
    stkIntA.push(9);
    
    int iTop = stkIntA.top();       //9
    stkIntA.top() = 19;         //19

    6.stackのサイズ
    stack.empty();   //        
    stack.size();        //       
    stack<int> stkIntA;
    stkIntA.push(1);
    stkIntA.push(3);
    stkIntA.push(5);
    stkIntA.push(7);
    stkIntA.push(9);
    
    if (!stkIntA.empty())
    {
        int iSize = stkIntA.size();     //5
    }
    stack vector deque at stack 。