开发者

how find an object in std::vector

I have an object like below

 class MyClass{
   int a;
   double b;
 };

and

 vector<MyClass> vMyClass;

then I want search in vMyClass try to use algorithm function:

 if( std::find(vMyClass.begin(), vMyClass.end(), aiField) == vMyClass.end())

I also defined operator==

 int operator ==(Field &Left,Field &Right)
 {
    return memcmp(&Left,&Right,sizeof(Field));
 }

or

int operator ==(Field &Right)
{
    return memcmp(this,&Right,sizeof(Field));
}

but I get an error message.

1>c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\algorithm(40) : error C2678: binary '==' : no operator found which takes a left-hand operand of type 'MyClass' (or there is no acceptable conversion)
1>        C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\guiddef.h(192): could be 'int operator ==(const GUID &,const GUID &)' [found using argument-dependent lookup]
1>        c:\users\shmuelian\documents\visual studio 2008\projects\src\examples\DLgenerator\DLParser.h(85): or       'int operator ==(MyClass &,MyClass &)' [found using argument-dependent lookup]
1>        c:\users\shmuelian\documents\visual studio 2008\projects\src\examples\DLgenerator\DLParser.h(70): or       'int MyClass::operator ==(MyClass &)'
1>        while trying to match the argument list '(MyClass, const MyClass)'
1>        c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\include\algorithm(74) : see reference to function template instantiation '_InIt std::_Find<std::_Vector_iterator<_Ty,_Alloc>,_Ty开发者_开发技巧>(_InIt,_InIt,const _Ty &)' being compiled
1>        with
1>        [
1>            _InIt=std::_Vector_iterator<MyClass,std::allocator<MyClass>>,
1>            _Ty=MyClass,
1>            _Alloc=std::allocator<MyClass>
1>        ]
1>        c:\users\shmuelian\documents\visual studio 2008\projects\src\examples\DLgenerator\DLParser.h(108) : see reference to function template instantiation '_InIt std::find<std::_Vector_iterator<_Ty,_Alloc>,MyClass>(_InIt,_InIt,const _Ty &)' being compiled
1>        with
1>        [
1>            _InIt=std::_Vector_iterator<MyClass,std::allocator<MyClass>>,
1>            _Ty=MyClass,
1>            _Alloc=std::allocator<MyClass>
1>        ]
1>CommandArgParser.cpp
1>Generating Code...


They need to be const references. And memcmp is really, really bad- just perform a member-wise comparison.


You forgot the const qualifier in your == operator, plus it should return bool, not int. Just use this instead:

bool operator ==(const Field &Left, const Field &Right)
{
    return (Left.a == Right.a) && (Left.b == Right.b);
}


You have a vector of MyClass, but an comparision operator for Field. You have to write an
bool operator == (const MyClass &, const MyClass &).

This operator should (as others indicated), not use memcmp, but rather perform a member-wise comparison.


Implement the operator within the class itself:

class MyClass
{    
public:
    int a;    
    double b;  

    bool operator ==(const MyClass &Right) const
    {      
        return (a == Right.a) && (b == Right.b);  // 'this' is left
    } 
}; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜