次の方法で共有


stack::operator<

Visual C++ で スタック :: operator< の標準テンプレート ライブラリ関数を使用する方法に (STL) ついて説明します。

template<class _TYPE, class _C, class _A>
   bool stack::operator<(
      const stack<_TYPE, _C, _A>& _X
   ) const;

解説

[!メモ]

プロトタイプのクラスやパラメーター名はヘッダー ファイルのバージョンと一致しない。ただし読みやすさが向上するように変更されました。

スタック :: operator< 関数の戻り値は演算子の左側の履歴スタックの値が右側の値よりも小さい場合にします。

1 個のスタックが別のスタックより小さいかどうかを確認するには

  1. 下部の要素 (スタックにプッシュされる一番最初の要素) を比較します。

  2. 要素が異なる場合小さい要素を持つスタックは大きな要素を持つスタック未満です。

  3. 要素が同じでより多くの要素がある場合はスタックの次の要素に移動し手順 2. に戻ります。

  4. スタックのすべての要素がこの時点で処理される場合スタックは同等です。

使用例

// StackLessThan.cpp
// compile with: /EHsc
// Illustrates how to use the stack::operator<
// function to determine if one stack is less than
// another stack.
//
// Functions:
//
//    operator< :  Returns true if the stack is smaller than the stack
//                 passed as the operand.
//////////////////////////////////////////////////////////////////////

#pragma warning(disable:4786)
#include <stack>
#include <iostream>

using namespace std ;

typedef stack<double> STACK_DOUBLE;

int main()
{
   STACK_DOUBLE stack1,stack2;

   // Add item 4.0 to Stack1. Stack1 contains 4.0.
   cout << "stack1.push(4.0)  s1=[4.0]" << endl;
   stack1.push(4.0);

   // Add item 3.0 to Stack1. Stack1 contains 3.0(top) and 4.0(bottom).
   cout << "stack1.push(3.0)  s1=[3.0 4.0]" << endl;
   stack1.push(3.0);

   // Add item 4.0 to Stack2. Stack2 contains 4.0 (top=bottom).
   cout << "stack2.push(4.0)  s2=[4.0]" << endl;
   stack2.push(4.0);

   // Compare if Stack1 is smaller than Stack2. Should return False.
   cout << "stack1<stack2 is " <<
      ((stack1<stack2)? "True": "False") << endl << endl;

   // Add item 6.0 to Stack2. Stack2 contains 6.0(top) and 4.0(bottom).
   cout << "stack2.push(6.0)  s2=[6.0 4.0]" << endl;
   stack2.push(6.0);

   // Compare if Stack1 is smaller than Stack2. Should return True.
   cout << "stack1<stack2 is " <<
      ((stack1<stack2)? "True": "False") << endl << endl;

   // Add item 8.0 to Stack2. Stack2 contains 8.0(top), 6.0 and
   // 4.0(bottom).
   cout << "stack2.push(8.0)  s2=[8.0 6.0 4.0]" << endl;
   stack2.push(8.0);

   // Compare if Stack1 is smaller than Stack2. Should return True.
   cout << "stack1<stack2 is " <<
      ((stack1<stack2)? "True": "False") << endl << endl;

   // Delete item 8.0 from Stack2.
   cout << "stack2.pop()      s2=[6.0 4.0]" << endl;
   stack2.pop();

   // Delete item 6.0 from Stack2.
   cout << "stack2.pop()      s2=[4.0]" << endl;
   stack2.pop();

   // Add item 3.0 to Stack2. Stack2 contains 3.0(top) and 4.0(bottom).
   cout << "stack2.push(3.0)  s2=[3.0 4.0]" << endl;
   stack2.push(3.0);

   // Compare if Stack1 is smaller than Stack2. Should return False.
   cout << "stack1<stack2 is " <<
      ((stack1<stack2)? "True": "False") << endl << endl;

   // Delete item 3.0 from Stack2.
   cout << "stack2.pop()      s2=[4.0]" << endl;
   stack2.pop();

   // Delete item 4.0 from Stack2.
   cout << "stack2.pop()      s2=[]" << endl;
   stack2.pop();

   // Add item 8.0 to Stack2. Stack2 contains 8.0(top=bottom).
   cout << "stack2.push(8.0)  s2=[8.0]" << endl;
   stack2.push(8.0);

   // Compare if Stack1 is smaller than Stack2. Should return True.
   cout << "stack1<stack2 is " <<
      ((stack1<stack2)? "True": "False") << endl << endl;
}

出力

stack1.push(4.0)  s1=[4.0]
stack1.push(3.0)  s1=[3.0 4.0]
stack2.push(4.0)  s2=[4.0]
stack1<stack2 is False

stack2.push(6.0)  s2=[6.0 4.0]
stack1<stack2 is True

stack2.push(8.0)  s2=[8.0 6.0 4.0]
stack1<stack2 is True

stack2.pop()      s2=[6.0 4.0]
stack2.pop()      s2=[4.0]
stack2.push(3.0)  s2=[3.0 4.0]
stack1<stack2 is False

stack2.pop()      s2=[4.0]
stack2.pop()      s2=[]
stack2.push(8.0)  s2=[8.0]
stack1<stack2 is True

必要条件

ヘッダー : <stack>

参照

概念

標準テンプレート ライブラリのサンプル