operator< 优先级队列错误
operator< error in priority queue
我遇到 "binary '<': no operator found which takes a left-hand operand of type 'const Baloons' (or there is no acceptable conversion)" 错误,我找不到任何解决方案?
我也想问一下如何按baloon.end排序优先队列?
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Baloons
{
public:
float start;
float end;
public:
Baloons() {}
Baloons(float start, float end) : start{ start }, end{ end } {}
};
int main()
{
Baloons baloon1(1, 5);
Baloons baloon2(4, 7);
Baloons baloon3(6, 9);
Baloons baloon4(11, 12);
std::priority_queue<Baloons> myBaloons;
myBaloons.push(baloon1);
myBaloons.push(baloon2);
myBaloons.push(baloon3);
myBaloons.push(baloon4);
while (!myBaloons.empty())
{
Baloons b = myBaloons.top();
cout << b.start << " -> " << b.end << " ";
myBaloons.pop();
}
system("pause");
return 0;
}
优先级队列是排序的容器,但是编译器不知道如何排序Baloons
。您必须定义自定义 operator<
以便对您的容器进行排序。
这显然是一道家庭作业题(很确定这只是为了 'myBaloons.pop()' 的笑话而创建的)所以我不想把所有的东西都扔掉......将这样的东西添加到你的气球 class.
bool operator<(const Baloons& rhs) const {
// return true if this Baloons is less than 'rhs'
}
这样你的气球就可以比较了。 (例如 if (baloon1 < baloon2) { /* do stuff */ }
)当您插入新对象时,优先级队列会在幕后进行这种比较。
你需要给你的气球class加一个operator<
,例如:
bool operator<(const Baloons& rhs) const {
return std::make_pair(start,end) < std::make_pair(rhs.start,rhs.end);
}
它将用于对您的 collection
中的元素进行排序
您需要为 Baloons
提供重载的 operator<
,否则编译器不知道如何对 std::priority_queue<Baloons>
的元素排序。例如,在 Baloons
class:
中定义这样的内容
bool operator<(const Baloons& _other) const {
return end < _other.end;
}
或任何您想在此处比较 return
语句的地方。
提供bool operator < (const Baloons&, const Baloons&)
,
bool operator < (const Baloons& lhs, const Baloons& rhs)
{
return lhs.end < rhs.end;
}
或者创建一个函子CustomComparer
并把它交给std::priority_queue<Ballons, std::vector<Ballons>, CustomComparer>
struct CustomComparer
{
bool operator () (const Baloons& lhs, const Baloons& rhs) const
{
return lhs.end < rhs.end;
}
};
以后
std::priority_queue<Ballons, std::vector<Ballons>, CustomComparer> myBaloons;
我遇到 "binary '<': no operator found which takes a left-hand operand of type 'const Baloons' (or there is no acceptable conversion)" 错误,我找不到任何解决方案?
我也想问一下如何按baloon.end排序优先队列?
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
class Baloons
{
public:
float start;
float end;
public:
Baloons() {}
Baloons(float start, float end) : start{ start }, end{ end } {}
};
int main()
{
Baloons baloon1(1, 5);
Baloons baloon2(4, 7);
Baloons baloon3(6, 9);
Baloons baloon4(11, 12);
std::priority_queue<Baloons> myBaloons;
myBaloons.push(baloon1);
myBaloons.push(baloon2);
myBaloons.push(baloon3);
myBaloons.push(baloon4);
while (!myBaloons.empty())
{
Baloons b = myBaloons.top();
cout << b.start << " -> " << b.end << " ";
myBaloons.pop();
}
system("pause");
return 0;
}
优先级队列是排序的容器,但是编译器不知道如何排序Baloons
。您必须定义自定义 operator<
以便对您的容器进行排序。
这显然是一道家庭作业题(很确定这只是为了 'myBaloons.pop()' 的笑话而创建的)所以我不想把所有的东西都扔掉......将这样的东西添加到你的气球 class.
bool operator<(const Baloons& rhs) const {
// return true if this Baloons is less than 'rhs'
}
这样你的气球就可以比较了。 (例如 if (baloon1 < baloon2) { /* do stuff */ }
)当您插入新对象时,优先级队列会在幕后进行这种比较。
你需要给你的气球class加一个operator<
,例如:
bool operator<(const Baloons& rhs) const {
return std::make_pair(start,end) < std::make_pair(rhs.start,rhs.end);
}
它将用于对您的 collection
中的元素进行排序您需要为 Baloons
提供重载的 operator<
,否则编译器不知道如何对 std::priority_queue<Baloons>
的元素排序。例如,在 Baloons
class:
bool operator<(const Baloons& _other) const {
return end < _other.end;
}
或任何您想在此处比较 return
语句的地方。
提供bool operator < (const Baloons&, const Baloons&)
,
bool operator < (const Baloons& lhs, const Baloons& rhs)
{
return lhs.end < rhs.end;
}
或者创建一个函子CustomComparer
并把它交给std::priority_queue<Ballons, std::vector<Ballons>, CustomComparer>
struct CustomComparer
{
bool operator () (const Baloons& lhs, const Baloons& rhs) const
{
return lhs.end < rhs.end;
}
};
以后
std::priority_queue<Ballons, std::vector<Ballons>, CustomComparer> myBaloons;