CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 17 of 17
  1. #16
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    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

  2. #17
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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[]?
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured