stack::operator==

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

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

备注

备注

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

例如,如果两堆栈具有封送的相等元素在同一个序列, 堆栈:: operator== 函数返回 true 。 例如,如果两堆栈是不同的大小, 堆栈:: operator== 函数始终返回 错误

示例

// StackEqual.cpp
// compile with: /EHsc
// Illustrates how to use the stack::operator==
// function to determine if two stacks are equal.
//
// Functions:
//    operator== :  Returns true if both stacks are the same.
//////////////////////////////////////////////////////////////////////

#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.
   cout << "stack1.push(4.0)  s1=[4.0]" << endl;
   stack1.push(4.0);

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

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

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

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

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

   // Keep adding item 8.0 to Stack2. Current Stack2 contains items
   // 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 Stack1 and Stack2. Should return False.
   cout << "stack1==stack2 is " <<
      ((stack1==stack2)? "True": "False") << endl << endl;

   // Delete the top item from Stack2. Current Stack2 contains items
   // 6.0 (top) and 4.0 (bottom).
   cout << "stack2.pop()      s2=[6.0 4.0]" << endl;
   stack2.pop();

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

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

   // Compare Stack2 and Stack2. Should return True.
   cout << "stack1==stack2 is " <<
      ((stack1==stack2)? "True": "False") << endl << endl;

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

   // Delete another item from Stack2. Stack2 should be empty.
   cout << "stack2.pop()      s2=[]" << endl;
   stack2.pop();

   // Push item 8.0 to Stack2.
   cout << "stack2.push(8.0)  s2=[8.0]" << endl;
   stack2.push(8.0);

   // Compare Stack1 and Stack2. Should return False.
   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 False

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

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

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

要求

**标题:**stack

请参见

概念

标准模板库示例