-
June 11th, 2005, 12:22 PM
#16
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.
Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html
-
June 11th, 2005, 04:41 PM
#17
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[]?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|