C++ STL  stack, another constructor 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++ stack to construct a stack that is empty or that is a copy of a base container object in C++ programming

To show: How to use the C++ stack, to construct a stack that is empty or that is a copy of a base container object in C++ programming

 

// C++ STL stack, another constructors example

#include <stack>

#include <vector>

#include <list>

#include <iostream>

using namespace std;

 

int main(void)

{

// declare stack with default deque base container

stack <char> dsc1;

// use or process the stack, explicitly declare a stack with deque base container

stack <char, deque<char> > dsc2;

// use or process the stack, declare a stack with vector base containers

stack <int, vector<int> > vsi1;

// use or process the stack, declare a stack with list base container

stack <int, list<int> > lsi;

// the second member function copies elements from a container

vector<int> v1;

 

// push data and then print the top

v1.push_back(5);

v1.push_back(3);

v1.push_back(7);

v1.push_back(9);

stack <int, vector<int> > vsi2(v1);

cout<<"The element at the top of vsi2 stack is "<<vsi2.top()<<endl;

return 0;

}

 

Output examples:

 

The element at the top of vsi2 stack is 9

Press any key to continue . . .

 

 

C++ STL  stack, constructors 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++ stack to construct a stack that is empty or that is a copy of a base container object in C++ programming

To show: How to use the C++ stack, constructor to construct a stack that is empty or that is a copy of a base container object in C++ programming

 

// C++ STL stack, constructors

#include <stack>

#include <vector>

#include <list>

#include <iostream>

using namespace std;

 

int main(void)

{

// declare stack with default deque base container

stack <char> deq1;

// explicitly declare a stack with deque base container

stack <char, deque<char> > deq2;

// declare a stack with vector base containers

stack <int, vector<int> > vec;

// declare a stack with list base container

stack <int, list<int> > lst;

cout<<endl;

return 0;

}

// no output

 

 

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