30 seconds of cpp: #2 - stack

Hussein Sarea

Hussein Sarea jul 23, 2022

4 min read 786 words

Introduction

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. Stack uses an encapsulated object of either vector or deque (by default) or list (sequential container class) as its underlying container, providing a specific set of member functions to access its elements.

30 Seconds of C++

emplace

Description : The emplace() member inserts an element at the top of the stack and constructs the element in place from the arguments provided. This can be more efficient than push in certain situations and avoid temporaries. emplace() effectively forwards its arguments to a constructor that is placed at the top of the stack.

emplace

This function is a feature introduced by the latest revision of the C++ standard (2011). Older compilers may not support it.

cpp

emplace_example.cpp

#include <iostream>
#include <stack>

class Employee{
    private:
        int id;
        std::string name;
    public:
        // Constructor
        Employee(int id, std::string name): id{id}, name{name} {}
        friend std::ostream& operator<<(std::ostream&, Employee&);
};
// Writes the member values of Employee to os
std::ostream& operator<<(std::ostream& out, Employee& obj){
    out << "ID: " << obj.id << std::endl;
    out << "Name: " << obj.name << std::endl;
    return out;
}
int main(){
    std::stack<Employee> st;
    st.emplace(1, "Hussein Sarea");
    st.emplace(2, "Moataz Sarea");
    while(!st.empty()){
        std::cout << st.top() << std::endl;
        st.pop();
    }
    return 0;
}

empty

Description : empty() function is used to check if the stack container is empty or not.

cpp

empty_example.cpp

#include <iostream>
#include <stack>

int main(){
    // Empty stack 
    std::stack<int> mystack; 
    //pushing elements using push()
    mystack.push(0); 
    mystack.push(1); 
    mystack.push(2); 
  
    while (!mystack.empty()) { 
        std::cout << ' ' << mystack.top(); // 2 1 0
        mystack.pop(); // deleting elements using pop()
    } 
    return 0;
}

pop

Description : pop() function is used to remove an element from the top of the stack(newest element in the stack). The element is removed to the stack container and the size of the stack is decreased by 1.

cpp

pop_example.cpp

#include <iostream>
#include <stack>

int main(){
    std::stack<int> st; 
    //pushing elements using push()
    for (int i = 1; i <= 5; ++i) st.push(i);
  
    while (!st.empty()) { 
        std::cout << ' ' << st.top(); // 5 4 3 2 1
        st.pop(); // deleting elements using pop()
    } 
    return 0;
}

push

Description : push() function is used to insert an element at the top of the stack. The element is added to the stack container and the size of the stack is increased by 1.

cpp

push_example.cpp

#include <iostream>
#include <stack>

int main(){
    std::stack<int> st; 
    //pushing elements using push()
    for (int i = 1; i <= 100; ++i) st.push(i);
  
    while (!st.empty()) { 
        std::cout << ' ' << st.top(); // 1 .. 100
        st.pop(); // deleting elements using pop()
    } 
    return 0;
}

size

Description : size() function is used to return the size of the satck or the number of elements in the stack.

cpp

size_example.cpp

#include <iostream>
#include <stack>

int main(){
    std::stack<int> st; 
    std::cout << "Size of st before pushing elements: " << st.size() << std::endl; // 0
    //pushing elements using push()
    for (int i = 1; i <= 100; ++i) st.push(i);
    std::cout << "Size of st after pushing elements: " << st.size() << std::endl; // 100
}

top

Description : top() function is used to reference the top(or the newest) element of the stack.

Member types reference and const_reference are aliases of the underlying container's types.

cpp

top_example.cpp

#include <iostream>
#include <stack>

int main (){
  std::stack<int> st;
  st.push(10);
  st.push(20);

  st.top() -= 5;

  std::cout << "st.top() is now " << st.top() << '\n'; // st.top() is now 15

  return 0;
}

swap

Description : swap() function is used to swap the contents of one stack with another stack of same type. Sizes may differ.

swap

This function is a feature introduced by the latest revision of the C++ standard (2011). Older compilers may not support it.

cpp

swap_example.cpp

#include <iostream>
#include <stack>

int main (){
    std::stack<int> foo,bar;
    foo.push (10);
    foo.push(20);
    foo.push(30);

    bar.push(111); 
    bar.push(222);

    std::cout << "Before swap: " << std::endl;
    std::cout << "size of foo: " << foo.size() << '\n';
    std::cout << "size of bar: " << bar.size() << '\n';

    foo.swap(bar);

    std::cout << "After swap: " << std::endl;
    std::cout << "size of foo: " << foo.size() << '\n';
    std::cout << "size of bar: " << bar.size() << '\n';

  return 0;
}

Wrapping up

If you like this then don't forget to share it ❤️. And I'll see you in the next article. See you soon.

Share on Social Media: