mardi 28 mars 2017

using stable_sort and passing an object as the custom comparison operator

This is part of an assignment, I am stuck at this instruction:

Sort your randomly generated pool of schedules. Use std::stable_sort, passing in an object of type schedule_compare as the custom comparison operator.


UPDATE: I was checking cppreference stable_srot(), see method definition below:

void stable_sort ( RandomAccessIterator first, RandomAccessIterator
 last,Compare comp );

, and it seems from what I understood is that you can only pass functions to the last argument (Compare comp) of the stable_sort() i.e:

However, in the instructions, it says that you need to pass an object of type schedule_compare. How is this possible ?


This is my code below:

struct schedule_compare
    {
        explicit schedule_compare(runtime_matrix const& m)
                : matrix_{m} { }

        bool operator()(schedule const& obj1, schedule const& obj2) {
            if (obj1.score > obj2.score)
                return true;
            else
                return false;
        }

    private:
        runtime_matrix const& matrix_;
    };

    auto populate_gene_pool(runtime_matrix const& matrix,
                            size_t const pool_size, random_generator& gen)
    {

        std::vector<schedule> v_schedule;
        v_schedule.reserve(pool_size);

        std::uniform_int_distribution<size_t> dis(0, matrix.machines() - 1);

        // 4. Sort your randomly generated pool of schedules. Use
        // std::stable_sort, passing in an object of type
        // schedule_compare as the custom comparison operator.

        std::stable_sort(begin(v_schedule), end(v_schedule), ???)

        return; v_schedule;
    }

Aucun commentaire:

Enregistrer un commentaire