结构的 std::sort 和 std::unique 问题
下面的代码:
#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>; &'从 'myStructDim'
我知道我必须覆盖一两个运算符。
请问是哪些以及具体如何?
感谢您的支持!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要比较运算符来表达“小于”和“等于”关系。定义独立的布尔函数
operator<
和operator==
,它们采用两个参数,每个参数const myStructDim&
,并以完全相同的方式执行比较您需要的,可能比将 then 定义为 struct 中的方法更简单。You need comparison operators to express the "less-than" and "equality" relationships. Defining stand-alone boolean functions
operator<
andoperator==
that take two arguments, eachconst myStructDim&
, and perform the comparison exactly the way you require, is probably simpler than defining then as methods within thestruct
.就像其他人提到的运算符<和运算符 == 可以解决问题,但我通常更喜欢传递比较谓词。
我在本例中使用 C++0x lambda,但没有它也可以实现。
Like others mentioned operator< and operator== would do the trick but I usually prefer to pass a comparision predicate.
I use C++0x lambdas in this example but it can be implemented without that.
您需要某种形式的比较函数来进行排序,并且需要某种形式的相等函数来进行唯一。
You need some form of comparison function for
sort
, and you need some form of equality function forunique
.如果没有operato>,是否不可能拥有某种独特性? ?我的意思是我可以理解,对于 unique 来说,我需要一个运算符==(就像苹果不是椅子一样),但是为什么椅子应该比苹果更大???我必须为某些没有意义的对象实现一个运算符!也许某种聚类会更有意义。
所以我决定为我自己实现这个问题,在我看来,这是一个更有意义的解决方案:
模板内联
void uniques(listtype In,listtype& Out)
{
Out.resize(In.size());
std::copy(In.begin(),In.end(),Out.begin());
listtype::iterator it = Out.begin();
listtype::iterator it2= Out.begin();
它2++;
int tmpsize = Out.size();
也许不是最好的解决方案,但目前我不知道更好的解决方案
Is it not possible to have some kind of unique without having the operato> ? I mean I can understand that for unique I need an operator== (like apples are not chairs) but why should a chair be greater than an apple ??? I would have to implement an operator for some objects where it makes no sense ! maybe some kind of clusering would make more sense.
So I decided to implement what the question is for my self here is in my opinion a solution that makes more sense:
template inline
void uniques(listtype In,listtype& Out)
{
Out.resize(In.size());
std::copy(In.begin(),In.end(),Out.begin());
listtype::iterator it = Out.begin();
listtype::iterator it2= Out.begin();
it2++;
int tmpsize = Out.size();
maybe not the best solution but at the moment I don t know betters