Guide for operator overloading in C++

pastaPicaso - Aug 18 - - Dev Community

Hi,
Operator overloading is very powerful feature in C++.
just by itself, it might not sound very powerful, but combine it with programming API provide by C++ stdlib, ( STL, standard template library ) and you have a wonderful programming experience.

I'll listdown a program that solved using operator overloading.

Problem: https://leetcode.com/problems/ugly-number-ii/description/

Its a decent level problem on leetcode, and there are many solution ways. One of them is using priority_queue. Priority_queue is a template class in STL and it can be used in various standard ways.

Here's the defination of the template class, according to the cppreference.

template<
    class T,
    class Container = std::vector<T>,
    class Compare = std::less<typename Container::value_type>
> class priority_queue;
Enter fullscreen mode Exit fullscreen mode

the first one is the type of Queue, or the items that will be queue'd. The second is the Container which defaults to vector. and third one is the comparator operator.

Now thats cool and all but I like to use it in a different way. using custom struct.

Here's an example.

       struct A { 
            long long number; 
            bool operator <(const struct A a) const { 
                return this->number > a.number; 
            }
            struct A operator()(int a) { 
                this->number = a; 
                return *this; 
            }
            long long operator* (int a) { 
                return this->number * a; 
            }
        };
        priority_queue<A> pq; 
Enter fullscreen mode Exit fullscreen mode

For a struct to be a valid Queue-able item, it has to have a Comparator operator overloaded, i.e the less-than operator. The Priority_queue class will use this information will use this function to order the elements in the Container class.

Nows whats the other 2 overloads for?
they are to easily cast the Wrapper type to Integer or Numeral type. Based on the complete solution you can access that I have used the wrapper type directly with Arithematic operations, Thats possible because of the Operator overloading symantics.

C++ is just great.
Here's the complete solutions for the Leetcode problem.

    int nthUglyNumber(int n) {
        struct A { 
            long long number; 
            bool operator <(const struct A a) const { 
                return this->number > a.number; 
            }
            struct A operator()(int a) { 
                this->number = a; 
                return *this; 
            }
            long long operator* (int a) { 
                return this->number * a; 
            }
        };
        priority_queue<A> pq; 
        pq.push(A(1)); 
        int cnt = 1; 
        set<int> done; 
        done.insert(1);
        while(!pq.empty()) { 
            A k = pq.top(); 
            cout << k.number << endl; 
            if (cnt == n) { 
                return (int)k.number;
            }
            cnt++;
            pq.pop();
            if (!done.count(k*2))
                pq.push(A(k * 2)),done.insert(k*2); 
            if (!done.count(k*3))
                pq.push(A(k * 3)),done.insert(k*3); 
            if (!done.count(k*5))
                pq.push(A(k * 5)),done.insert(k*5); 

        }
        return 0;
    }
Enter fullscreen mode Exit fullscreen mode
. . . .
Terabox Video Player