C++ STL stack, operator!= program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Using the C++ operator!= to test if the stack object on the left side of the operator is not equal to the stack object on the right side in C++ programming

To show: How to use the C++ stack, operator!= to test if the stack object on the left side of the operator is not equal to the stack object on the right side in C++ programming

 

// C++ STL stack, operator!=

#include <stack>

#include <vector>

#include <iostream>

using namespace std;

int main(void)

{

// declare stacks with vector base containers

stack <int, vector<int> > s1, s2, s3;

int j;

 

// the following would have cause an error because stacks with different

// base containers are not equality comparable

//

// stack <int, list<int> > s3;

//

// push/insert data

s1.push(1);

s1.push(2);

s1.push(3);

s2.push(2);

s3.push(1);

s3.push(2);

s3.push(3);

 

// print the data

cout<<"s1 stack data: ";

for(j = s1.top(); j != int(s1.empty()); j--)

cout<<j<<' ';

cout<<endl;

cout<<"s2 stack data: ";

for(j = s2.top(); j != int(s2.empty()); j--)

cout<<j<<' ';

cout<<endl;

cout<<"s3 stack data: ";

for(j = s3.top(); j != int(s3.empty()); j--)

cout<<j<<' ';

cout<<endl;

cout<<"\nOperation: s1 != s2?"<<endl;

if (s1 != s2)

cout<<"The stacks s1 and s2 are not equal."<<endl;

else

cout<<"The stacks s1 and s2 are equal."<<endl;

cout<<"Operation: s1 != s3?"<<endl;

if (s1 != s3)

cout<<"The stacks s1 and s3 are not equal."<<endl;

else

cout<<"The stacks s1 and s3 are equal."<<endl;

return 0;

}

 

Output examples:

 

s1 stack data: 3 2 1

s2 stack data: 2 1

s3 stack data: 3 2 1

Operation: s1 != s2?

The stacks s1 and s2 are not equal.

Operation: s1 != s3?

The stacks s1 and s3 are equal.

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index