0

I'm a newbie and this is my first question. So I am working for a task organizer and I want to organize list of tasks by their "urgency" value. Here is my code:

#include <iostream>
#include <math.h>
#include <vector>
#include <stdlib.h>
#include <list>

using namespace std;

struct Task {
public:
    string name;
    float deadline, estimated;
    int urgency;
    int getUrgency() {
        urgency = ceil(deadline - estimated);
        return urgency;
    }
};

void newTask(Task task[], int n1) {
    for (int i = 0; i < n1; i++)
    {
        system("cls");
        cout << "Input task name: ";
        cin >> task[i].name;
        cout << "Input task deadline (in hours): ";
        cin >> task[i].deadline;
        cout << "Input task estimated work time (in hours): ";
        cin >> task[i].estimated;
    }
}

void printAll(Task task[], int n1) {
    system("cls");
    cout << "Name\tDeadline\tEstimated\tUrgency\n";
    for (int i = 0; i < n1; i++)
    {
        cout << task[i].name << "\t" << task[i].deadline << "\t\t" << task[i].estimated << "\t\t" << task[i].getUrgency() << endl;
    }
}

int main() {
    int n;
    cout << "How many work do you have? ";
    cin >> n;
    //Create number of object based on input n
    std::vector<Task> p(n);
    newTask(p.data(), n);
    std::list<Task> taskList;
    printAll(p.data(), n);
    cin.ignore();
    return 0;
}

I want to add a function that sorts the list of tasks by their "urgency" value. What kind of function should I use?

athanasia
  • 3
  • 2
  • related/dupe: https://stackoverflow.com/questions/1380463/sorting-a-vector-of-custom-objects – NathanOliver Sep 24 '21 at 19:42
  • 1
    [`std::priority_queue`](https://en.cppreference.com/w/cpp/container/priority_queue) springs immediately to mind, but read up on it first as its scope is very narrow. `print_all`, for example, becomes grossly complicated. – user4581301 Sep 24 '21 at 19:43

2 Answers2

1

I would try using std::sort. It will sort in place any iterable object (array, vector, etc...). A common std::sort function call has the following arguments: The first argument is an iterator/pointer to the beginning of the collection, the second argument is an iterator/pointer to the end of that same collection, and the third argument is a function callback that determines how the data is sorted. You can see an example implementation here

  • You don't need to copy the data to a `std::vector`, the `std::sort` algorithm also works with arrays. Pointers work as random-access iterators. – Blastfurnace Sep 24 '21 at 19:58
  • @Blastfurnace I was not aware of that. I will update my answer, thanks! :) – caleb-bender Sep 24 '21 at 19:59
  • Tactical note: Stack Overflow users tend to frown on links to cplusplus.com because cplusplus tends to favour easy of reading over accuracy of the material. [cppreference's documentation](https://en.cppreference.com/w/cpp/algorithm/sort) is generally much more precise, at the cost of looking like it was translated from Martian. If cplusplus's advice doesn't seem to work, switch to cppreference. In the long term, you'll find yourself using cpprefference more often because right almost always trumps easy. – user4581301 Sep 24 '21 at 20:07
  • @user4581301 I appreciate the advice. I use cppreference when I want an exhaustive look at a topic. While cplusplus.com is not up to date in much of its documentation and it does not tend to be rigorous, I thought that the example code for `std::sort` was helpful in gleaning a basic understanding. – caleb-bender Sep 24 '21 at 20:32
1

In your case you can use the std::sort function, defined in <algorithm> header, on the p vector defining a custom compare function:

std::sort (p.begin(), p.end(), sortTaskByUrgency);

where sortTaskByUrgency() is defined as:

bool sortTaskByUrgency(const Task& lhs, const Task& rhs)
{
    return lhs.getUrgency() < rhs.getUrgency();
}

Using the above function in your sample code getUrgency() must be const:

int getUrgency() const { return ceil(deadline - estimated); }

removing useless int urgency public member.

Carlo Banfi
  • 186
  • 1
  • 7
  • The code gave me this error: ```the object has type qualifiers that are not compatible with the member function "Task::getUrgency"``` with red lines under lhs.getUrgency() and rhs.getUrgency(). – athanasia Sep 28 '21 at 02:50
  • Please redefine getUrgency() function to make it `const`: `int getUrgency() const { return ceil(deadline - estimated); }` – Carlo Banfi Sep 28 '21 at 11:12
  • @athanasia I edited the answer considering your note. – Carlo Banfi Sep 28 '21 at 18:29