[英]std::sort and std::unique problem with a struct
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'
错误C2784:'布尔std :: operator ==(const std :: vector <_Ty,_Alloc>&,const std :: vector <_Ty,_Alloc>&)':无法推断'const std :: vector的模板参数<_Ty,_Alloc>&'来自'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
. 定义
const myStructDim&
两个参数(每个const myStructDim&
的独立布尔函数operator<
和operator==
,并按照所需的方式执行比较,可能比在struct
定义为方法简单。
您需要某种形式的比较函数来进行sort
,而您需要某种形式的相等函数来进行unique
。
Like others mentioned operator< and operator== would do the trick but I usually prefer to pass a comparision predicate. 像其他提到的一样,operator <和operator ==可以解决问题,但我通常更喜欢传递比较谓词。
I use C++0x lambdas in this example but it can be implemented without that. 在此示例中,我使用了C ++ 0x lambda,但是没有它也可以实现。
std::sort(
vec_dim.begin(),
vec_dim.end(),
[] (myStructDim const & l, myStructDim const & r) {return l.nId < r.nId;}
);
vec_dim.erase(
std::unique(
vec_dim.begin(),
vec_dim.end(),
[] (myStructDim const & l, myStructDim const & r) {return l.nId == r.nId;}
),
vec_dim.end()
);
Is it not possible to have some kind of unique without having the operato> ? 如果没有operator>,就不可能有某种独特性吗? 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 ???
我的意思是我可以理解,要获得唯一性,我需要一个operator ==(就像苹果不是椅子),但是为什么椅子要比苹果大? 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()); 模板内联无效唯一性(列表类型输入,列表类型和输出){Out.resize(In.size()); std::copy(In.begin(),In.end(),Out.begin());
std :: copy(In.begin(),In.end(),Out.begin()); listtype::iterator it = Out.begin();
listtype :: iterator it = Out.begin(); listtype::iterator it2= Out.begin();
listtype :: iterator it2 = Out.begin(); it2++;
it2 ++; int tmpsize = Out.size();
int tmpsize = Out.size();
while(it!=Out.end())
{
it2 = it;
it2++;
while((it2)!=Out.end())
{
if ((*it)==(*it2))
Out.erase(it2++);
else
++it2;
}
it++;
}
}
maybe not the best solution but at the moment I don t know betters 也许不是最好的解决方案,但目前我还不知道更好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.