std :: sort和std :: unique结构的唯一问题 [英] std::sort and std::unique problem with a struct
问题描述
以下代码:
#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()
);
}
不会编译出许多错误,例如:
错误C2784:'bool std :: operator
==(const std :: vector< _Ty,_Alloc>&,const std :: vector< ; _Ty,_Alloc>&)':
无法推导
的模板参数const std :: vector< _Ty,_Alloc>&'from
'myStructDim'
我明白我必须重写一个或两个运算符。
感谢您的支持!
运算符<
和运算符==
,每个 const myStructDim&
,并按照你所需的方式执行比较,可能比在 struct
中定义then作为方法更简单。 The following code:
#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()
);
}
will not compile with many errors, such as:
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'
I understand that I have to override an operator or two.
Which ones and how exactly please?
Thanks for the support!
You need comparison operators to express the "less-than" and "equality" relationships. Defining stand-alone boolean functions operator<
and operator==
that take two arguments, each const myStructDim&
, and perform the comparison exactly the way you require, is probably simpler than defining then as methods within the struct
.
这篇关于std :: sort和std :: unique结构的唯一问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!