std::find [By any chance my question is not clear?]
-
In the below example, I just push some pointers to objects of type myClass into the vector. Previously, When I pushed just the objects as such, the find algo worked with the overloaded "==". But it's now pointers inside the vector. And I want the find algo still to find it, with integer as the passed argument for it. How can I do it?
class myClass
{
public:
int x;
int y;
bool operator == (const int& n)
{
cout<<"\nCheck";
cout<<"\n"<<n<<"\t"<<x;
return(n==x);}
};
std::vector<myClass\*> vec\_myClass; myClass\* obj = new myClass(); obj->x=1; obj->y=1; vec\_myClass.push\_back(obj); myClass\* obj2= new myClass(); obj2->x=2; obj2->y=2; vec\_myClass.push\_back(obj2); int n=2; vector<myClass\*>::iterator itr\_vec = vec\_myClass.begin(); itr\_vec =std::find(vec\_myClass.begin(),vec\_myClass.end(),n);//Compilation error here.
itr_vec =std::find(vec_myClass.begin(),vec_myClass.end(),n);
Here it's not taking "n" , and I can also guess why isn't taking. It actually expects an overload of == where we pass a pointer to the object itself. Is there anyway I can do it? Just by passing int?---------------------------- 286? WOWW!:-O
modified on Saturday, March 15, 2008 1:37 PM
-
In the below example, I just push some pointers to objects of type myClass into the vector. Previously, When I pushed just the objects as such, the find algo worked with the overloaded "==". But it's now pointers inside the vector. And I want the find algo still to find it, with integer as the passed argument for it. How can I do it?
class myClass
{
public:
int x;
int y;
bool operator == (const int& n)
{
cout<<"\nCheck";
cout<<"\n"<<n<<"\t"<<x;
return(n==x);}
};
std::vector<myClass\*> vec\_myClass; myClass\* obj = new myClass(); obj->x=1; obj->y=1; vec\_myClass.push\_back(obj); myClass\* obj2= new myClass(); obj2->x=2; obj2->y=2; vec\_myClass.push\_back(obj2); int n=2; vector<myClass\*>::iterator itr\_vec = vec\_myClass.begin(); itr\_vec =std::find(vec\_myClass.begin(),vec\_myClass.end(),n);//Compilation error here.
itr_vec =std::find(vec_myClass.begin(),vec_myClass.end(),n);
Here it's not taking "n" , and I can also guess why isn't taking. It actually expects an overload of == where we pass a pointer to the object itself. Is there anyway I can do it? Just by passing int?---------------------------- 286? WOWW!:-O
modified on Saturday, March 15, 2008 1:37 PM
_8086 wrote:
It actually expects an overload of == where we pass a pointer to the object itself. Is there anyway I can do it? Just by passing int?
Atleast one formal parameter of operators must be of non pointer class type, otherwise you can simply add a global operator == for type "myClass *" and int. But you can achieve it very easily with a another class which exposes the overloaded operator. say,
class CSearchKey
{
public:
int m_iVal;
CSearchKey(int iVal)
:m_iVal (iVal)
{
};
};inline bool operator == (const myClass *pObj, const CSearchKey &rKey)
{
return(*pObj==rKey.m_iVal); // calls myclass==int
}And in your original code you only want to change as follows.
itr_vec =std::find(vec_myClass.begin(),vec_myClass.end(),CSearchKey(n));
hope this solves.
-
_8086 wrote:
It actually expects an overload of == where we pass a pointer to the object itself. Is there anyway I can do it? Just by passing int?
Atleast one formal parameter of operators must be of non pointer class type, otherwise you can simply add a global operator == for type "myClass *" and int. But you can achieve it very easily with a another class which exposes the overloaded operator. say,
class CSearchKey
{
public:
int m_iVal;
CSearchKey(int iVal)
:m_iVal (iVal)
{
};
};inline bool operator == (const myClass *pObj, const CSearchKey &rKey)
{
return(*pObj==rKey.m_iVal); // calls myclass==int
}And in your original code you only want to change as follows.
itr_vec =std::find(vec_myClass.begin(),vec_myClass.end(),CSearchKey(n));
hope this solves.
Hello Rajkumar, thanks for your effort. I get these following errors. How did you manage to compile? I'm using VC8.0
error C2804: binary 'operator ==' has too many parameters
error C2333: 'myClass::operator ==' : error in function declaration; skipping function bodyThe whole program:
class CSearchKey
{
public:
int m_iVal;
CSearchKey(int iVal)
:m_iVal (iVal)
{
};
};class myClass
{
public:
int x;
int y;
/*
bool operator == (const int& n)
{
cout<<"\nCheck";
cout<<"\n"<<n<<"\t"<<x;
return(n==x);} \*/ inline bool operator == (const myClass \*pObj, const CSearchKey &rKey) { return(\*pObj==rKey.m\_iVal); // calls myclass==int }
};
int _tmain(int argc, _TCHAR* argv[])
{std::vector<myClass\*> vec\_myClass; myClass\* obj = new myClass(); obj->x=1; obj->y=1; vec\_myClass.push\_back(obj); myClass\* obj2= new myClass(); obj2->x=2; obj2->y=2; vec\_myClass.push\_back(obj2); int n=2; vector<myClass\*>::iterator itr\_vec = vec\_myClass.begin(); itr\_vec =std::find(vec\_myClass.begin(),vec\_myClass.end(),CSearchKey(n)); if(itr\_vec!=vec\_myClass.end()) cout<<"\\n\\nResult:"<<(\*itr\_vec)->x<<"\\n\\n"; return 0;
}
---------------------------- 286? WOWW!:-O
-
Hello Rajkumar, thanks for your effort. I get these following errors. How did you manage to compile? I'm using VC8.0
error C2804: binary 'operator ==' has too many parameters
error C2333: 'myClass::operator ==' : error in function declaration; skipping function bodyThe whole program:
class CSearchKey
{
public:
int m_iVal;
CSearchKey(int iVal)
:m_iVal (iVal)
{
};
};class myClass
{
public:
int x;
int y;
/*
bool operator == (const int& n)
{
cout<<"\nCheck";
cout<<"\n"<<n<<"\t"<<x;
return(n==x);} \*/ inline bool operator == (const myClass \*pObj, const CSearchKey &rKey) { return(\*pObj==rKey.m\_iVal); // calls myclass==int }
};
int _tmain(int argc, _TCHAR* argv[])
{std::vector<myClass\*> vec\_myClass; myClass\* obj = new myClass(); obj->x=1; obj->y=1; vec\_myClass.push\_back(obj); myClass\* obj2= new myClass(); obj2->x=2; obj2->y=2; vec\_myClass.push\_back(obj2); int n=2; vector<myClass\*>::iterator itr\_vec = vec\_myClass.begin(); itr\_vec =std::find(vec\_myClass.begin(),vec\_myClass.end(),CSearchKey(n)); if(itr\_vec!=vec\_myClass.end()) cout<<"\\n\\nResult:"<<(\*itr\_vec)->x<<"\\n\\n"; return 0;
}
---------------------------- 286? WOWW!:-O
the newly suggested operator is to be in global scope not in the class myclass, and you commented the already existing operator but that is used in the global == operator. Don't you see == operator takes multiple parameter its not a class member. keep your code unchanged, don't comment the == operator in myclass. then the move the inline == operator to global, just below the CSearchKey class, use it in a header so that if you are using it in multiple cpp file this becomes inline and no additional code is generated for this. only thing you have to modify in your original code is whenever you search for int n search with CSearchKey(n). Try and let me know.
-
the newly suggested operator is to be in global scope not in the class myclass, and you commented the already existing operator but that is used in the global == operator. Don't you see == operator takes multiple parameter its not a class member. keep your code unchanged, don't comment the == operator in myclass. then the move the inline == operator to global, just below the CSearchKey class, use it in a header so that if you are using it in multiple cpp file this becomes inline and no additional code is generated for this. only thing you have to modify in your original code is whenever you search for int n search with CSearchKey(n). Try and let me know.
Great that's works! Thanks a lot. But why is not taking int as a parameter for == ? What harms it? For example,
inline bool operator == (const myClass *pObj,const int& n) { return(pObj->x==n); } itr_vec =std::find(vec_myClass.begin(),vec_myClass.end(),n);
instead of,inline bool operator == (const myClass *pObj,const CSearchKey &rKey) { return(return(pObj->x ==rKey.m_iVal);); } itr_vec =std::find(vec_myClass.begin(),vec_myClass.end(),CSearchKey(n));
Why does it require a paramter of class type??operator ==' must have at least one formal parameter of class type
I dont understand this requirement.
---------------------------- 286? WOWW!:-O
-
Great that's works! Thanks a lot. But why is not taking int as a parameter for == ? What harms it? For example,
inline bool operator == (const myClass *pObj,const int& n) { return(pObj->x==n); } itr_vec =std::find(vec_myClass.begin(),vec_myClass.end(),n);
instead of,inline bool operator == (const myClass *pObj,const CSearchKey &rKey) { return(return(pObj->x ==rKey.m_iVal);); } itr_vec =std::find(vec_myClass.begin(),vec_myClass.end(),CSearchKey(n));
Why does it require a paramter of class type??operator ==' must have at least one formal parameter of class type
I dont understand this requirement.
---------------------------- 286? WOWW!:-O
if both parameters are pointers or primitive types it takes pure comparison of values. (int and void *).
_8086 wrote:
But why is not taking int as a parameter for == ?
you can have wrapper for "myClass *", ptrMyClass as we did for int, CSearchKey. then inline bool operator == (const myClass *pObj,const int& n) can be used as inline bool operator == (const ptrMyClass &ptrObj,const int& n) but then you have to store in vector as vector<ptrmyclass>.