Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 没有默认构造函数作为模板参数的自定义比较器_C++_Priority Queue - Fatal编程技术网

C++ 没有默认构造函数作为模板参数的自定义比较器

C++ 没有默认构造函数作为模板参数的自定义比较器,c++,priority-queue,C++,Priority Queue,让我们看一个从两个排序数组中查找最小的m对数字的玩具示例。 撇开算法效率问题不谈,我想为优先级队列提供一个比较器 在初始化过程中需要某些参数: class Foo { struct natural_order { const std::vector<int> &x,&y; natural_order( const std::vector<int> &x, const std::vector<int>

让我们看一个从两个排序数组中查找最小的
m
对数字的玩具示例。 撇开算法效率问题不谈,我想为优先级队列提供一个比较器 在初始化过程中需要某些参数:

class Foo {
    struct natural_order {
        const std::vector<int> &x,&y;
        natural_order( const std::vector<int> &x, const std::vector<int> &y ): x(x), y(y) {};
        bool operator () ( const std::pair<int,int> &a, const pair<int,int> &b ) const {
            return x[a.first]+y[a.second] < x[b.first]+y[b.second];
        }
    };
    struct cmp {
        std::unique_ptr<natural_order> o;
        cmp( const std::vector<int> &x, const std::vector<int> &y ) {
           o= std::make_unique<natural_order>(x,y);
        }
        bool operator () ( const std::pair<int,int> &a, const pair<int,int> &b ) const {
            return (*o)(b,a);
        }
    };
public:
    std::vector<std::vector<int>> m_smalles_pairs( std::vector<int> &x, std::vector<int> &y, int m ) {
        std::vector<std::vector<int>> res(m);
        std::priority_queue<int,std::vector<int>,cmp(x,y)> pq; //<-- the problem is here. Does not compile
        for ( int i= 0; i < m; ++i ) {
            auto pr= pq.top(); pq.pop();
            res[i]= std::vector<int>{x[pr.first],y[pr.second]};
            if ( pr.first+1 < x.size() )
                pq.push({pr.first+1,pr.second});
            if ( pr.second+1 < y.size() )
                pq.push({pr.first,pr.second+1});
        }
        return res;
    }
};
class-Foo{
结构自然顺序{
常数std::向量&x,&y;
自然序(常数std::vector&x,常数std::vector&y):x(x),y(y){};
bool运算符()(const std::pair&a、const pair&b)const{
返回x[a.first]+y[a.second]std::priority_queue pq;//本质上,代码的问题在于比较的实现。
从技术上讲,比较类必须满足以下要求:

类型T满足-->CopyConstructible

您的类
cmp
不可复制构造,因为它的一个成员是
std::unique\u ptr
(无法复制)

所以我想说,一个合适的解决方案应该是按照这个原则重构您的设计。 我不知道你的问题的全部领域,所以我不能建议什么是正确的设计(而且这可能是个人的选择)

也许,您可以从类中删除
std::unique_ptr
,只需将
natural_order
类型作为成员。当然,这意味着不同的复制构造调用

在这一点上,您可以使用适当的构造函数初始化您的比较器

(2) explicit priority_queue(const Compare& compare)
           : priority_queue(compare, Container()) { }
您的代码应该类似于:

std::priority_queue<int,std::vector<int>,cmp> pq(cmp{x, y});
std::优先级队列pq(cmp{x,y});