Reputation: 241
I have a class, the constructor looks like this:
myclass(int=0,string="",int=0,string="",int=0,int=0,
int=0,int=0,string="",int=0,int=0);
and a vector with elements of this type
vector<myclass>myvect;
the vector is sorted and I am trying to remove duplicates and this is not working:
std::vector<myclass>::iterator it;
it=std::unique (myvect.begin(), myvect.end());
myvect.resize(std::distance(myvect.begin(),it) );
I get this error
:algorithm(1862): error C2678: binary '==' :
no operator found which takes a left-hand operand
of type 'myclass' (or there is no acceptable conversion)
any idea why? is there any way I can remove duplicates from this vector?
Upvotes: 0
Views: 1979
Reputation: 110648
The std::unique
algorithm needs to know how to compare two myclass
objects for equality. There are two ways you can do this. The first is to implement myclass::operator==
. The second is to pass a binary predicate to std::unique
:
std::unique (myvect.begin(), myvect.end(),
[](const myclass& a, const myclass& b) {
return /* some expression to test equality */;
});
Upvotes: 3
Reputation: 23624
You need to overload the operator ==
for your class myclass
in order to apply the unique
algorithm.
Quoting from std::unique documentation:
The function uses operator== to compare the pairs of elements (or pred, in version (2)).
Upvotes: 1