Проблема с использованием приоритетных очередей с использованием моего собственного созданного класса - PullRequest
0 голосов
/ 28 мая 2019

Я создал триплетный класс и хочу использовать его объект в качестве членов в очереди Min Priority, но получаю ошибку.Код для объединения k отсортированных массивов.Пожалуйста, помогите мне решить это сомнение.Я пытался решить эту проблему несколькими способами, но в большинстве случаев получал ошибку.

    #include<queue>
    using namespace std;

    class triplet{
        public:
        int element;
        int ai;
        int ei;
        bool operator()(triplet &a, triplet &b);
    };


    class Comp{
        public:
        bool operator()(triplet &a, triplet &b){    
            return a.element > b.element;       
        }
    };

    vector<int> mergeKSortedArrays(vector<vector<int>*> input){
        /* Don't write main().
         * Don't read input, it is passed as function argument.
         * Return the output.
         * Taking input and printing output is handled automatically.
         */
        vector<int> ans;
        priority_queue<triplet , vector<triplet> , Comp> pq;

        for(int i = 0; i < input.size(); i++){
            triplet *temp = new triplet();
            temp->element = input[i]->at(0);
            temp->ai = i;
            temp->ei = 0;
            pq.push(temp);
        }

        while(!pq.empty()){
            triplet current = pq.top();
            ans.push_back(current);
            pq.pop();

            if(current.ei < input[current.ai]->size())
                pq.push_back(input[current.ai]->at(current.ei + 1) , current.ai , ei + 1);
        }
        return ans;
    }

Compilation Failed
In file included from Runner.cpp:4:0:
Solution.h: In function 'std::vector mergeKSortedArrays(std::vector*>)':
Solution.h:34:21: error: no matching function for call to 'std::priority_queue, Comp>::push(triplet*&)'
         pq.push(temp);
                     ^
In file included from /usr/include/c++/5/queue:64:0,
                 from Solution.h:1,
                 from Runner.cpp:4:
/usr/include/c++/5/bits/stl_queue.h:499:7: note: candidate: void std::priority_queue<_Tp, _Sequence, _Compare>::push(const value_type&) [with _Tp = triplet; _Sequence = std::vector; _Compare = Comp; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = triplet]
       push(const value_type& __x)
       ^
/usr/include/c++/5/bits/stl_queue.h:499:7: note:   no known conversion for argument 1 from 'triplet*' to 'const value_type& {aka const triplet&}'
/usr/include/c++/5/bits/stl_queue.h:507:7: note: candidate: void std::priority_queue<_Tp, _Sequence, _Compare>::push(std::priority_queue<_Tp, _Sequence, _Compare>::value_type&&) [with _Tp = triplet; _Sequence = std::vector; _Compare = Comp; std::priority_queue<_Tp, _Sequence, _Compare>::value_type = triplet]
       push(value_type&& __x)

1 Ответ

1 голос
/ 28 мая 2019

temp - это указатель на triplet, но ваша приоритетная очередь хранит триплет объектов .Измените temp на triplet temp; (внесите другие соответствующие изменения кода).Или дайте triplet конструктор, полностью избавьтесь от temp и используйте вместо него pq.emplace(/*...*/);.

...