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
}
The C++ standard library comes with a set of find functions.
You are looking for
find_ifwhich takes a functor that specifies the comparison.You can also use a lambda: