我尝试创建与一起使用的自定义类std::set。我知道我需要为此提供一个自定义比较器,因此我使过载operator<。但是,当我尝试使用代码复制集时set<Edge> a; set<Edge> b = a;,出现以下错误:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/__functional_base:63:21:对二进制表达式无效的操作数(“ const Edge”和“ const Edge”)
class Edge {
public:
Edge(int V, int W, double Weight):v(V),w(W),weight(Weight){}
int other(int vertex){ return v ? w : vertex == w;}
int v,w;
double weight;
friend std::ostream& operator<<(std::ostream& out, const Edge& e)
{
out<<e.v<<' '<<e.w<<' '<<"weight:"<<e.weight<<'\n';
return out;
}
bool operator<(const Edge& other)
{
return weight < other.weight;
}
};
Run Code Online (Sandbox Code Playgroud)
使
bool operator<(const Edge& other) const
Run Code Online (Sandbox Code Playgroud)
因为必须标记比较运算符const。中的键std::set是const,因此在实例operator<上调用const,因此必须标记const。
| 归档时间: |
|
| 查看次数: |
310 次 |
| 最近记录: |