std::map.find
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: std::map.find

  1. #1
    Join Date
    Jan 2009
    Posts
    25

    std::map.find

    Hey,

    Just wondering... if I'm storing my user defined type in a stl container and I use find... does find invoke the user defined types == operator in its search? (So I must define the == operator?)

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: std::map.find

    No, std::map's find() makes use of operator< instead, or the given predicate that is used to compare the keys to put them in the correct order. That said, if you are going to provide operator< it probably makes sense to provide operator== as well.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Oct 2008
    Location
    Singapore
    Posts
    195

    Re: std::map.find

    Quote Originally Posted by laserlight View Post
    No, std::map's find() makes use of operator< instead, or the given predicate that is used to compare the keys to put them in the correct order. That said, if you are going to provide operator< it probably makes sense to provide operator== as well.
    Hi Laserlight,

    Why do you say that he should provide operator== as well? Because he may use STL algorithms?

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,266

    Re: std::map.find

    Quote Originally Posted by rohshall
    Why do you say that he should provide operator== as well? Because he may use STL algorithms?
    That is one consideration, but what I had in mind was providing a complete interface. It just seems natural to me that if operator< is available then operator== should be available as well even though operator< is enough to determine equality. (But the converse is not true: operator== cannot be used to implement operator<, and that operator== is provided does not necessarily make it natural to provide operator< since it might not make sense, e.g., in the case of input iterators.)
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center