Denis
Denis

Reputation: 3707

How to use default comparator in own class?

I want to create own data container like STL-containers.

template <class priorityType = size_t, class Compare = std::less<priorityType>>
class task_queue
{
public:
    task_queue(Compare c = Compare())
    {

    }

private:
    std::priority_queue<priorityType, std::vector<priorityType>, Compare> tasks_id;
};

int main() {
    struct foo
    {
        int a;
    };

    struct foo_compare
    {
        bool operator()(const foo& lhs, const foo& rhs) const {
            return lhs.a < rhs.a;
        }
    };

    task_queue<foo, foo_compare> queue{ foo_compare() };
}

I want to use comparator, which is passed to constructor, in tasks_id PQ. How can I do this?

Upvotes: 0

Views: 165

Answers (2)

W.F.
W.F.

Reputation: 13988

You just need to invoke it:

c(valuetocompare1, valuetocompare2);

It's simple as that.

Upvotes: 0

ForEveR
ForEveR

Reputation: 55897

Just call right constructor.

task_queue(Compare c = Compare()) : tasks_id(c)
{

}

Upvotes: 2

Related Questions