Microsoft KB Archive/158037

= How to use the stack::operator== STL function in Visual C++ =

Article ID: 158037

Article Last Modified on 8/11/2005

-

APPLIES TO

 The Standard C++ Library, when used with:  Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

 Microsoft Visual C++ 6.0 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q158037

<div class="summary_section">

SUMMARY
The sample code below illustrates how to use the stack::operator== STL function in Visual C++.

<div class="moreinformation_section">

Prototype
template<class _TYPE, class _C, class _A> bool stack::bool operator==(const stack<_TYPE, _C, _A>& _X) const; NOTE: The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Description
The stack::operator== function returns true if both stacks have equal elements arranged in the same sequence. The stack::operator== function always returns false if the two stacks are of different size.

Sample Code
////////////////////////////////////////////////////////////////////// // // Compile options needed: /GX // // StackEqual.cpp : 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. // // Written by Derek Jamison // of Microsoft Product Support Services, // Copyright (c) 1996 Microsoft Corporation. All rights reserved. //////////////////////////////////////////////////////////////////////


 * 1) pragma warning(disable:4786)

using namespace std;
 * 1) include
 * 2) include

using namespace std; // std c++ libs implemented in std #endif
 * 1) if _MSC_VER > 1020  // if VC++ version is > 4.2

typedef stack<double, deque<double, allocator >,

allocator > STACK_DOUBLE;

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

}

Program 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

<div class="references_section">