vector find / operator ==

Posted on 2004-11-14
Last Modified: 2013-12-14

Trying to iron out more misunderstandings.

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

class data {
   int id;
   data(int idx) : id(idx) { }
   data(data*);  //conversion constructor
   bool operator== (const data& data_)  const
     return ( == 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!!!

  vector<data*>::iterator search = find(dataset.begin(), dataset.end(),
   if(search != dataset.end())
        cout << dataset[search - dataset.begin()]<<endl;
     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:

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
Question by:forums_mp
    1 Comment
    LVL 15

    Accepted Solution

    a == b

    is equivalent to

    a.operator ==(b)


    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()]".


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    Title # Comments Views Activity
    Help with my python script 6 135
    maxMirror challenge 10 67
    Problem to setup 18 65
    mapBully challenge 6 50
    Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
    How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from ( Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
    The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
    The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now