stack::operator<

在 Visual C++ 演示如何使用 堆栈:: operatorAMP_LT 标准 (STL)模板库函数。

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

备注

备注

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

,如果在运算符左侧的堆栈大于右侧,的堆栈小于 堆栈:: operatorAMP_LT 函数返回 true。

确定堆栈是否大于另一个堆栈小于

  1. 比较这个最深的元素 (第一个元素推入堆栈)。

  2. 如果元素不同,以较小的元素的堆栈与具有最大元的堆栈小于。

  3. 如果元素是相同的,并且具有多个元素中,移动到堆栈中的下一个元素并返回步骤。

  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;
}

Output

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

请参见

概念

标准模板库示例