Skip to content

Instantly share code, notes, and snippets.

@denkiwakame
Created November 11, 2016 01:59
Show Gist options
  • Save denkiwakame/8e20e4bb0d2d429470cd4760c99ad123 to your computer and use it in GitHub Desktop.
Save denkiwakame/8e20e4bb0d2d429470cd4760c99ad123 to your computer and use it in GitHub Desktop.
よく忘れる
// 内からコンパレータ
struct msg {
float res;
float x;
float y;
cv::Vec<float, _LABEL_MAX_NUM_> values;
msg() : res(0.0) {};
msg(float res_) : res(res_) {};
msg(int x_, int y_) : res(0.0), x(x_), y(y_) {};
bool operator<(const msg& m) const { return res > m.res; };
};
std::priority_queue<msg> pq;
// 外からコンパレータ
struct LessThanByRes {
bool operator()(const msg& m1, const msg& m2) const {
return m1.res > m2.res; // ASC
}
};
// external operator
std::priority_queue<msg, std::vector<msg>, LessThanByRes> pq;
// なんか生やしたい場合
// ※ 実際は削除フラグ方式でpopしたときにdeleteした方が良い
template<typename T, typename Container = std::vector<T>, typename Compare = std::less<typename Container::value_type>>
class custom_priority_queue : public std::priority_queue<T, std::vector<T>, std::less<T>>
{
public:
bool remove(const T& value) {
auto it = std::find(this->c.begin(), this->c.end(), value);
if (it != this->c.end()) {
this->c.erase(it);
std::make_heap(this->c.begin(), this->c.end(), this->comp);
return true;
}
else {
return false;
}
}
};
template<typename T, typename Container = std::vector<T>, typename Compare = std::less<typename Container::value_type>>
class custom_priority_queue : public std::priority_queue<T, std::vector<T>, std::less<T>>
{
public:
bool remove(const T& value) {
auto it = std::find(this->c.begin(), this->c.end(), value);
if (it != this->c.end()) {
this->c.erase(it);
std::make_heap(this->c.begin(), this->c.end(), this->comp);
return true;
}
else {
return false;
}
}
};
// cにアクセスしたい場合
// 特殊化では触れぬ
class MyPQ : std::priority_queue<msg, msg, PQ::LessThanByRes>
{
public:
bool remove(const int x, const int y, const int msg_id) {
auto it = std::find_if(this->c.begin(), this->c.end(), [&](resbp::msg& m){ return m.x == x && m.y == y && m.msg_id == msg_id;});
if (it != this->c.end()) {
this->c.erase(it);
return true;
}
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment