Re: How to use find() to find a struct member in vector?
First, use the proper algorithm, search() is used to find subsequences: http://www.sgi.com/tech/stl/search.html
find() is what you're looking for when searching for 1 element: http://www.sgi.com/tech/stl/find.html
Next, your MatchMember is not (necessarily) correct when comparing strings:
Code:
bool MatchMember(member m1, member m2)
{
return strcmp(m1.name,m2.name)&&m1.age==m2.age&&m1.sex==m2.sex;
}
strcmp returns 0 when both strings are equal, however, 0 is also considered false. So when both strings are equal, your function is returning 0, indicating false, meaning no match. To correct, negate the string comparison result.
Re: How to use find() to find a struct member in vector?
My code works ok, it's just that you wrote the MatchMember the wrong way:
Code:
bool MatchMember(member m1, member m2)
{
return strcmp(m1.name,m2.name) ==0 && m1.age==m2.age && m1.sex==m2.sex;
}
Why don't you use std::string instead of char[]?