Interview style solution✅💯 | C++ OOPS | Beginner friendly 
CodeImportant FAQ's1. Do you need a comparator for the set with a custom struct?Yes, in C++, when you create a std::set for a custom structure, you typically ne

# Code
```cpp []
struct Task_info{
    int priority, task_id, user_id;
    //Parametrized constructor
    Task_info(int a, int b, int c){
        priority = a;
        task_id = b;
        user_id = c;
    }
    //Explicit default constructor
    Task_info() : priority(0), task_id(0), user_id(0) {}
};

//struct for Comparison inside set
struct Comparator {
    bool operator()(const Task_info& lhs, const Task_info& rhs) const {
        if (lhs.priority != rhs.priority) return lhs.priority > rhs.priority;
        return lhs.task_id > rhs.task_id;
    }
};

class TaskManager {
public:
    set<Task_info, Comparator> s;
    map<int, Task_info> m;

    TaskManager(vector<vector<int>>& tasks) {
        for(auto j:tasks){
            Task_info t = Task_info(j[2], j[1], j[0]);
            s.insert(t);
            m[j[1]] = t;
        }
    }
    
    void add(int userId, int taskId, int priority) {
        Task_info t = Task_info(priority, taskId, userId);
        s.insert(t);
        m[taskId] = t;
    }
    
    void edit(int taskId, int newPriority) {
        Task_info t = m[taskId];
        if(s.find(t) != s.end()){
            s.erase(s.find(t));
        }
        t.priority = newPriority;
        s.insert(t);
        m[taskId] = t; 
    }
    
    void rmv(int taskId) {
        Task_info t = m[taskId];
        if(s.find(t) != s.end()){
            s.erase(s.find(t));
        }
        m.erase(taskId);      
    }
    
    int execTop() {
        if(s.empty()){
            return -1;
        }
        auto task = *s.begin();
        s.erase(s.begin());
        return task.user_id;
    }
};

/**
 * Your TaskManager object will be instantiated and called as such:
 * TaskManager* obj = new TaskManager(tasks);
 * obj->add(userId,taskId,priority);
 * obj->edit(taskId,newPriority);
 * obj->rmv(taskId);
 * int param_4 = obj->execTop();
 */

```
# Important FAQ's
### 1. Do you need a comparator for the set with a custom struct?
Yes, in C++, when you create a std::set for a custom structure, you typically need to provide a comparator to define the ordering of elements in the set. By default, std::set uses std::less to compare its elements, but it doesn't know how to compare custom structures unless you specify the comparison logic.

Define a custom comparator either:
- As a standalone function. (done in code above)
- As a functor (struct with operator()).
```
struct MyStruct {
    int a, b, c;

    bool operator<(const MyStruct& other) const {
        // Custom comparison logic (e.g., lexicographical order by a, then b, then c)
        if (a != other.a) return a < other.a;
        if (b != other.b) return b < other.b;
        return c < other.c;
    }
};
```
- Using a lambda function when defining the set.
```
struct MyStruct {
    int a, b, c;
};

int main() {
    auto comparator = [](const MyStruct& lhs, const MyStruct& rhs) {
        if (lhs.a != rhs.a) return lhs.a < rhs.a;
        if (lhs.b != rhs.b) return lhs.b < rhs.b;
        return lhs.c < rhs.c;
    };

    std::set<MyStruct, decltype(comparator)> mySet(comparator);

    mySet.insert({1, 2, 3});
    mySet.insert({1, 2, 2});
    mySet.insert({2, 0, 0});

    for (const auto& elem : mySet) {
        std::cout << elem.a << " " << elem.b << " " << elem.c << '\n';
    }

    return 0;
}
```

### 2. How does `m[j[1]] = t` work ? / why is default constructor required in Task_info ?
 - When you use operator[], if the key (j[1] in this case) doesn't exist, the map will try to default-construct the value. If Task_info doesn't have a default constructor, the map cannot create a default object, leading to a compilation error.
 -  If you provide a parameterized constructor (i.e., a constructor that takes arguments), the compiler will not automatically generate a default constructor. This means that if you need default construction (for example, in cases like std::map::operator[]), you must explicitly define a default constructor.
- After std::map default-constructs the value for the missing key, the copy constructor is used during the assignment m[j[1]] = t. The copy constructor copies the values of t into the default-constructed object.

### If you like the above solution or found something insightful, pls do upvote.🙌


['Hash Table', 'Design', 'Heap (Priority Queue)', 'Ordered Set', 'C++']