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?

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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().
Please redefine getUrgency() function to make it const: int getUrgency() const { return ceil(deadline - estimated); }
@athanasia I edited the answer considering your note.
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

4 Comments

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 I was not aware of that. I will update my answer, thanks! :)
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 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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.