Friday, September 20, 2024

stack

Overview
The standard library provides stack adapter to provide LIFO (Last In First Out) operations where elements are inserted and extracted only from one end of the container.

Details
stack is basically an adapter based on associative containers such as deque or vector  or list that provides  operations  such as push_back() and pop_back().

Syntax
The syntax is as below. Template parameter represents the datatype to store and Container represents the container used for storage and operations. vectordeque and list are possible candidates.
template < class T, class Container = deque<T> > 
class stack;

Members
 It defines following types
NameDescription
value_typeThe first template parameter (T)
container_typeThe second template parameter (Container)
referencevalue_type&
const_referenceconst value_type&
size_typean unsigned integral type

Operation
stack can be graphically represented as below.

New elements are stored in the host container. Operations such as push and pop are supported where elements are added or removed from the top.

Complexity
The complexity of push and pop operations are O(1).

Functionality

Constructors
In following constructors use default allocator. Custom allocators can be used by passing them as an additional argument.

NameDescription
stack()Default Constructor. Default container is deque.

Example:
//deque
//v:{}
stack<int> v;

//v2:{}
stack<int,vector<int>> v2;

//v3:{}
stack<int,list<int>> v3;
stack(const container_type& c)Constructs a container from container c.

Example:
//v:{4,3,2,1}
stack<int> v2(deque<int>{1,2,3,4});

//v2:{4,3,2,1}
stack<int,vector<int>> v2(vector<int>{1,2,3,4});

//v3:{4,3,2,1}
stack<int,list<int>> v3(list<int>{1,2,3,4});
stack(const stack& x)copy constructor. Note that underlying container should be case.

Example:
//v:{4,3,2,1}
stack<int,vector<int>> v(vector<int>{1,2,3,4});

//v2:{4,3,2,1}
stack<int,vector<int>> v2(v);
stack(stack&& x)move constructor. Note that underlying container should be case.

Example:
//v:{4,3,2,1}
stack<int,vector<int>> v(vector<int>{1,2,3,4});

//v:{}
//v2:{4,3,2,1}
stack<int,vector<int>> v2(move(v));

Capacity
NameDescription
size_type size() Returns the number of elements by calling size() method of the container.
Example:
//v:{4,3,2,1}
stack<int> v(deque<int>{1,2,3,4});
//prints 4 cout << v.size();
bool empty()Tests whether stack is empty by calling empty() method of the container .

Element Access
NameDescription
reference top()Returns reference to the last element by calling back() method on container.  
Example:
//v:{4,3,2,1}
vector<int> v{1,2,3,4};

//prints 4. 
cout << v.top(); 

Modifiers
NameDescription
void push(const value_type& val)
void push(value_type&& val)
Adds element at the top by calling push_back() method of the container.

Example:
stack<int> v{};
//v:{1}
v.push(1);
void pop()Deletes the element at the top by calling pop_back() method of the container.

Example:
stack<int> v{1};
//v:{}
v.pop();
void swap(stack& v)Swap content with v. Note T has to be same.

Example:
stack<int> v{1,2,3};
stack<int> v2{4,5,6};
//v2:{3,2,1}
//v:{6,5,4}
v2.swap(v);
void clear()Clears the contents.

Example:
vector<int> v{1,2,3,4,5};
//v:{}
v.clear();
void emplace(Args ...arg)Constructs and inserts element at the top using arg by calling emplace_back() method of the container.

Example:
stack<int> v{1,2,3,4};
//v:{5,4,3,2,1}
v.emplace(5);

No comments:

Post a Comment