vector find / operator ==


Trying to iron out more misunderstandings.

Consider:
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class data {
public:
   int id;
   data(int idx) : id(idx) { }
   data(data*);  //conversion constructor
   bool operator== (const data& data_)  const
   {
     return (data_.id == id);
   };
 };

bool operator==(data *&a, const data &b)
{
  return *a==b;
};

int main()
{
  data* a = new data(1);
  data* b = new data(2);
  data* c = new data(3);
  data* element = new data(3);

  vector<data*> dataset;  
  dataset.push_back(a);    // should be using smat pointer here per the text.. Investigate!!!
  dataset.push_back(b);
  dataset.push_back(c);

  vector<data*>::iterator search = find(dataset.begin(), dataset.end(),
                                                          *element);
   if(search != dataset.end())
   {
        cout<<"found"<<endl;
        cout << dataset[search - dataset.begin()]<<endl;
    }
   else
   {
     cout<<"can't find"<<endl;
   }
}

For this to work I'm not understanding why i need the function:
  bool operator==(data *&a, const data &b)
Now given:
  data a;
  data b;
  a = b;
This equates to:
  a.operator(b);  

Whats the equivalent for operator==(data *&a, const data &b) when called via the find algorithm?

2.  I'd like to print the value but  " dataset[search - dataset.begin()] " gives me the address
forums_mpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

efnCommented:
a == b

is equivalent to

a.operator ==(b)

or

operator ==(a, b)

If that doesn't answer your question, please clarify it.

2.  There is some kind of iterator difference function in the standard library, but it would be simpler just to dereference the iterator.  search is like a pointer to an element of dataset, so *search is a pointer to a data, and *search gives you what you are trying to get with " dataset[search - dataset.begin()]".

--efn
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.