Let's look at a toy example of finding the smallest m
pairs of numbers from two sorted arrays.
The algorithm efficiency issue aside, I would want to supply the priority queue with a comparator
that needs certain parameters during initialization:
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;
}
};
Essentially, I would like the comparator to be initialized with an argument, but how to supply this as the third argument of priority_queue
? If the x
and y
params were not needed, I would simply write cmp
in the above.
EDIT: it appears to proceed in similar lines using lambdas, as described here C++ priority_queue with lambda comparator error
I was able to solve my problem, but just being curious if struct
-comparator allows this sort of thing.