我的比较器有什么问题?如何解决?
What's wrong with my comparator? How to fix it?
我想从点向量中的每个点找到 2d 点的 k 个最近邻居。比较器定义为class,标准是向量的每个点到查询点的距离。
MVW:
#include<iostream>
#include<fstream>
#include<functional>
#include<algorithm>
#include<vector>
#include<deque>
#include<queue>
#include<set>
#include<list>
#include<limits>
#include<string>
#include<memory>
using namespace std;
class random_Point_CCS_xy_generator;
class Point_CCS_xy{
private:
long double x_coordinate_ {0.0};
long double y_coordinate_ {0.0};
public:
Point_CCS_xy () = default;
Point_CCS_xy(long double x, long double y);
~Point_CCS_xy() = default;
Point_CCS_xy(const Point_CCS_xy& cpyObj);
Point_CCS_xy(Point_CCS_xy&& moveObj) noexcept;
Point_CCS_xy& operator=(const Point_CCS_xy& l_val_RHS);
Point_CCS_xy& operator=(Point_CCS_xy&& r_val_RHS) noexcept;
__attribute__((unused)) inline void set_x_coordinate(long double x);
__attribute__((unused)) inline void set_y_coordinate(long doubley);
__attribute__((unused)) inline long doubleget_x_coordinate() const;
__attribute__((unused)) inline long doubleget_y_coordinate() const;
bool operator<(const Point_CCS_xy& pnt) const;
bool operator==(const Point_CCS_xy& RHS) const;
long double direct_pnt_to_pnt_distance(const Point_CCS_xy& a, const Point_CCS_xy& b) const;
long double squared_direct_pnt_to_pnt_distance(const Point_CCS_xy& a, const Point_CCS_xy& b) const;
long double distance_to_this_pnt_sq(const Point_CCS_xy& other) const;
void print_point_xy () const;
friend ostream& operator<<(ostream& os, const Point_CCS_xy& pnt);
vector<Point_CCS_xy> Random_Point_CCS_xy(long double Min, long double Max, size_t n);
vector<shared_ptr<Point_CCS_xy>> Random_Point_CCS_xy2_(long double Min, long double Max, size_t n);
};
// the comparator under question
class Less_Distance : public std::binary_function<Point_CCS_xy, Point_CCS_xy, bool> {
Point_CCS_xy& point_;
public:
explicit Less_Distance(Point_CCS_xy& reference_point)
: point_(reference_point) {}
bool operator () (const Point_CCS_xy& a, const Point_CCS_xy& b) const {
return point_.distance_to_this_pnt_sq(a) < point_.distance_to_this_pnt_sq(b);
}
};
int main() {
vector<Point_CCS_xy> points {
move(Point_CCS_xy(68.83402637, 38.07632221)),
move(Point_CCS_xy(76.84704074, 24.9395109)),
move(Point_CCS_xy(16.26715795, 98.52763827)),
move(Point_CCS_xy(70.99411985, 67.31740151)),
move(Point_CCS_xy(71.72452181, 24.13516764)),
move(Point_CCS_xy(17.22707611, 20.65425362)),
move(Point_CCS_xy(43.85122458, 21.50624882)),
move(Point_CCS_xy(76.71987125, 44.95031274)),
move(Point_CCS_xy(63.77341073, 78.87417774)),
move(Point_CCS_xy(8.45828909, 30.18426696))
};
cout << "we are here" << endl;
for(auto p : points) {
cout << p << endl;
}
Point_CCS_xy inquiry_pnt(16.452, 70.258);
Less_Distance nearer_point(inquiry_pnt);
vector<Point_CCS_xy> nn_points;
// Problematic part of the code:
priority_queue<Point_CCS_xy, vector<Point_CCS_xy>, nearer_point> pQ;
for(auto pnt : points) {
pQ.push(pnt);
}
while(!pQ.empty()){
nn_points.push_back(move(pQ.top()));
pQ.pop();
}
for(auto p : nn_points) {
cout << p << endl;
}
return 0;
}
错误是Template argument for template type parameter must be a type
。由于我是新手,任何评论都会有所帮助,因此不胜感激。
排序参数必须是类型,但您传递的是对象。
你在构建队列时传递了一个这种类型的参数。
priority_queue<Point_CCS_xy, vector<Point_CCS_xy>, Less_Distance> pQ(nearer_point);
我想从点向量中的每个点找到 2d 点的 k 个最近邻居。比较器定义为class,标准是向量的每个点到查询点的距离。
MVW:
#include<iostream>
#include<fstream>
#include<functional>
#include<algorithm>
#include<vector>
#include<deque>
#include<queue>
#include<set>
#include<list>
#include<limits>
#include<string>
#include<memory>
using namespace std;
class random_Point_CCS_xy_generator;
class Point_CCS_xy{
private:
long double x_coordinate_ {0.0};
long double y_coordinate_ {0.0};
public:
Point_CCS_xy () = default;
Point_CCS_xy(long double x, long double y);
~Point_CCS_xy() = default;
Point_CCS_xy(const Point_CCS_xy& cpyObj);
Point_CCS_xy(Point_CCS_xy&& moveObj) noexcept;
Point_CCS_xy& operator=(const Point_CCS_xy& l_val_RHS);
Point_CCS_xy& operator=(Point_CCS_xy&& r_val_RHS) noexcept;
__attribute__((unused)) inline void set_x_coordinate(long double x);
__attribute__((unused)) inline void set_y_coordinate(long doubley);
__attribute__((unused)) inline long doubleget_x_coordinate() const;
__attribute__((unused)) inline long doubleget_y_coordinate() const;
bool operator<(const Point_CCS_xy& pnt) const;
bool operator==(const Point_CCS_xy& RHS) const;
long double direct_pnt_to_pnt_distance(const Point_CCS_xy& a, const Point_CCS_xy& b) const;
long double squared_direct_pnt_to_pnt_distance(const Point_CCS_xy& a, const Point_CCS_xy& b) const;
long double distance_to_this_pnt_sq(const Point_CCS_xy& other) const;
void print_point_xy () const;
friend ostream& operator<<(ostream& os, const Point_CCS_xy& pnt);
vector<Point_CCS_xy> Random_Point_CCS_xy(long double Min, long double Max, size_t n);
vector<shared_ptr<Point_CCS_xy>> Random_Point_CCS_xy2_(long double Min, long double Max, size_t n);
};
// the comparator under question
class Less_Distance : public std::binary_function<Point_CCS_xy, Point_CCS_xy, bool> {
Point_CCS_xy& point_;
public:
explicit Less_Distance(Point_CCS_xy& reference_point)
: point_(reference_point) {}
bool operator () (const Point_CCS_xy& a, const Point_CCS_xy& b) const {
return point_.distance_to_this_pnt_sq(a) < point_.distance_to_this_pnt_sq(b);
}
};
int main() {
vector<Point_CCS_xy> points {
move(Point_CCS_xy(68.83402637, 38.07632221)),
move(Point_CCS_xy(76.84704074, 24.9395109)),
move(Point_CCS_xy(16.26715795, 98.52763827)),
move(Point_CCS_xy(70.99411985, 67.31740151)),
move(Point_CCS_xy(71.72452181, 24.13516764)),
move(Point_CCS_xy(17.22707611, 20.65425362)),
move(Point_CCS_xy(43.85122458, 21.50624882)),
move(Point_CCS_xy(76.71987125, 44.95031274)),
move(Point_CCS_xy(63.77341073, 78.87417774)),
move(Point_CCS_xy(8.45828909, 30.18426696))
};
cout << "we are here" << endl;
for(auto p : points) {
cout << p << endl;
}
Point_CCS_xy inquiry_pnt(16.452, 70.258);
Less_Distance nearer_point(inquiry_pnt);
vector<Point_CCS_xy> nn_points;
// Problematic part of the code:
priority_queue<Point_CCS_xy, vector<Point_CCS_xy>, nearer_point> pQ;
for(auto pnt : points) {
pQ.push(pnt);
}
while(!pQ.empty()){
nn_points.push_back(move(pQ.top()));
pQ.pop();
}
for(auto p : nn_points) {
cout << p << endl;
}
return 0;
}
错误是Template argument for template type parameter must be a type
。由于我是新手,任何评论都会有所帮助,因此不胜感激。
排序参数必须是类型,但您传递的是对象。
你在构建队列时传递了一个这种类型的参数。
priority_queue<Point_CCS_xy, vector<Point_CCS_xy>, Less_Distance> pQ(nearer_point);