Question: Trying to define a find function for my vector because the vector holds multiple data; it's a vector of a struct

I'm taking input of an ID, and am trying to search that in my Table and find its index (if that ID already exists)

So I have the declarations here:

```
vector<Employee> Table;
vector<Employee>::iterator It;
vector<Employee>::iterator find_It;

//Table has these values
//Table.ID, Table.ch1, Table.ch2

```

And I'm trying to find the ID here: 

```
cin >> update_ID;
find_It = find(Table.begin(), Table.end(), update_ID);

```

Would there be a way to do the find with the variable update\_ID?

I tried doing this:

```
find_It = find(Table.begin(), Table.end(), (*It).update_ID;

```

but obviously my vector Employee doesn't have that data member named update\_ID

The other option I was thinking of doing is creating my own find function, which I'm a little confused on how to define

I want to return the index of the ID where Table.ID = update\_ID

What do I put as the return type and value parameters? Is it

```
returntype find( Iterator, Iterator, update ID)
{ 
    for (vector<Employee>::iterator myit = Table.begin(), Table.end(), myit++)
    {
        if update_ID == Table.ID
        {
            return myit;
        }
    }
    return myit
}

```

Answer: 
The C++ standard library comes with [a set of find functions](http://en.cppreference.com/w/cpp/algorithm/find).

You are looking for `find_if` which takes a functor that specifies the comparison.

```
// a functor taking the update_ID you 
// are looking for as an argument in the constructor
struct myfind {
  myfind(int needle) : needle(needle) {}

  int needle;
  bool operator()(const Employee& x) {
    return x.ID == needle;
  }
};

// use as
int update_ID = 23;
std::find_if(begin(Table), end(Table), myfind(update_ID));

```

You can also use a lambda:

```
int id;
std::find_if(begin(Table), end(Table),
             [=](const Employee& x) { return x.update_ID == id; });

```
You can use your own matching function by employing [find\_if](http://www.cplusplus.com/reference/algorithm/find_if/).
I assume that in your first snippet, you are referring to Employee having the members ID, ch1, ch2, not Table.
One way to solve the problem would be this:

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

using std::cout;
using std::endl;
using std::vector;

struct Employee{
   int ID;
   int ch1;
   int ch2;
};

int IDmatch;

bool match_id( Employee myemp){
   return myemp.ID==IDmatch;
}

int main(){

   vector<Employee> Table;

   // fill example vector
   Employee temp; // use this for inserting structs into your vector
   for(int i=0; i<10; i++){
      temp.ID = i; // 1,2,3,...
      temp.ch1 = 10*i+1; // 11,21,32,...
      temp.ch2 = 10*i+2; // 12,22,32,...
      Table.push_back(temp);
   }

   vector<Employee>::iterator itv;
   IDmatch = 3;
   itv = find_if(Table.begin(), Table.end(), match_id);
   cout << "found an Employee with ID=" << IDmatch << ": ch1=" << itv->ch1 << ", ch2=" << itv->ch2 << endl;

}

```