我有一个复杂的结构,我希望将其作为std :: map的一个关键字来快速生成所有唯一对象的列表:
union somecomplexstruct {
struct {
more_structs val1, val2;
even_more_structs val3, val4;
lots_of_more_structs val5;
};
unsigned int DATA[3];
};
typedef map<somecomplexstruct, int, greater<somecomplexstruct> > somecomplexstructMap;
Run Code Online (Sandbox Code Playgroud)
但它说错误: error C2784: 'bool std::operator >(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'const somecomplexstruct'
我如何使我的结构在那里工作?
编辑:得到它的工作,感谢大家!下面是代码:
inline bool operator>(const somecomplexstruct &v1, const somecomplexstruct &v2){
if(v1.DATA[0] > v2.DATA[0]) return 1;
if(v1.DATA[0] < v2.DATA[0]) return 0;
if(v1.DATA[1] > v2.DATA[1]) return 1;
if(v1.DATA[1] < v2.DATA[1]) return 0;
return v1.DATA[2] > v2.DATA[2];
}
Run Code Online (Sandbox Code Playgroud)
std::greater<>调用operator>()来完成它的工作,所以你需要重载它,如果你想使用它std::greater<>.
它应该如下所示:
inline bool operator>(const somecomplexstruct& lhs, const somecomplexstruct& rhs)
{
// implement your ordering here.
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1764 次 |
| 最近记录: |