Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. std::find [By any chance my question is not clear?]

std::find [By any chance my question is not clear?]

Scheduled Pinned Locked Moved C / C++ / MFC
questiongraphicshelptutorial
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _8086
    wrote on last edited by
    #1

    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

    R 1 Reply Last reply
    0
    • _ _8086

      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

      R Offline
      R Offline
      Rajkumar R
      wrote on last edited by
      #2

      _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.

      _ 1 Reply Last reply
      0
      • R Rajkumar R

        _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.

        _ Offline
        _ Offline
        _8086
        wrote on last edited by
        #3

        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 body

        The 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

        R 1 Reply Last reply
        0
        • _ _8086

          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 body

          The 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

          R Offline
          R Offline
          Rajkumar R
          wrote on last edited by
          #4

          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.

          _ 1 Reply Last reply
          0
          • R Rajkumar R

            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.

            _ Offline
            _ Offline
            _8086
            wrote on last edited by
            #5

            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

            R 1 Reply Last reply
            0
            • _ _8086

              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

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              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>.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups