cplusplus.com cplusplus.com
cplusplus.com   C++ : Reference : STL Containers : stack : stack
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forum
Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
stack
comparison operators
stack::stack
member functions:
· stack::empty
· stack::pop
· stack::push
· stack::size
· stack::top

-

stack::stack public member function
explicit stack ( const Container& ctnr = Container() );

Construct stack

Constructs a stack container adaptor object.
A container adaptor keeps a container object as data. This container object is a copy of the argument passed to the constructor, if any, otherwise it is an empty container.

Parameters

ctnr
Container object
Container is the second class template parameter (the type of the underlying container for the stack; by default: deque<T>, see class description).

Example

// constructing stacks
#include <iostream>
#include <vector>
#include <deque>
#include <stack>
using namespace std;

int main ()
{
  deque<int> mydeque (3,100);     // deque with 3 elements
  vector<int> myvector (2,200);   // vector with 2 elements

  stack<int> first;               // empty stack
  stack<int> second (mydeque);    // stack initialized to copy of deque

  stack<int,vector<int> > third;  // empty stack using vector
  stack<int,vector<int> > fourth (myvector);

  cout << "size of first: " << (int) first.size() << endl;
  cout << "size of second: " << (int) second.size() << endl;
  cout << "size of third: " << (int) third.size() << endl;
  cout << "size of fourth: " << (int) fourth.size() << endl;

  return 0;
}
Output:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2

Complexity

Constant (one container construction). Although notice that the container construction itself may not take constant time.

© The C++ Resources Network, 2000-2007 - All rights reserved
Spotted an error? - contact us