以下代码:
#include <vector>
#include <algorithm>
struct myStructDim
{
int nId;
int dwHeight;
int dwWidth;
};
void main()
{
::std::vector<myStructDim> m_vec_dim;
::std::sort(m_vec_dim.begin(), m_vec_dim.end());
m_vec_dim.erase(
::std::unique(m_vec_dim.begin(), m_vec_dim.end()),
m_vec_dim.end()
);
}
不会编译,有很多错误,例如:
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 'myStructDim'
我知道我必须覆盖一两个运算符。
请问具体是哪些?
感谢支持!
最佳答案
您需要比较运算符来表达“小于”和“等于”关系。定义独立的 bool 函数 operator<
和 operator==
有两个参数,每个参数 const myStructDim&
, 并完全按照您需要的方式执行比较,可能比将 then 定义为 struct
中的方法更简单。 .
关于c++ - std::sort 和 std::unique 结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3640663/