CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    std::map containing a struct of four doubles as key

    Hello all,

    I have a struct of four doubles and I want to use it as a key to std::map

    Code:
    struct MatPropVals
    {
       double ambient;
       double diffuse;
       double specular;
       double emission;
    }
    How do I write the comparing function. I know I have to be careful about comparing doubles. Can somebody please help me. Before this I have only use std::map with simple types.

    I don't want to insert a new MatPropVals object if it has already been inserted with all the four double values being equal. std::map will take care of this. But how do I write the comparing function.

    I know that there is a FAQ about comparing doubles (epsilon thing). Does it apply in my case as well. I will take care of this but just need help on the comparing function.

    Thanks in advance
    If there is no love sun won't shine

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: std::map containing a struct of four doubles as key

    The simplest method is to provide operator < for the class.

    Code:
    struct MatPropVals
    {
       double ambient;
       double diffuse;
       double specular;
       double emission;
    
       bool operator < (const MatPropVals & rhs) const
       {
           if (ambient < rhs.ambient) return true;
           if (ambient > rhs.ambient) return false;
    
           if (diffuse < rhs.diffuse) return true;
           if (diffuse > rhs.diffuse) return false;
    
           if (specular < rhs.specular) return true;
           if (specular > rhs.specular) return false;
    
           return emission < rhs.emission;
       }
    };
    You could probably add the "epsilon thing" to the above code. I
    would have to think about how to implement that.
    Last edited by Philip Nicoletti; April 7th, 2008 at 09:30 AM.

  3. #3
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: std::map containing a struct of four doubles as key

    Thanks Philip,

    But I think I cannot use it

    Actually my struct is like this:

    Code:
    #pragma pack( push, 1 )
    struct MatPropVals
    {
       double ambient;
       double diffuse;
       double specular;
       double emission;
    }
    #pragma pack( pop )
    In order to read the material property values I directly pass the object's address. It's a COM function I don't have control over it.

    Could you please give me a compare function example?
    If there is no love sun won't shine

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: std::map containing a struct of four doubles as key

    Code:
    struct MyCompare
    {
        bool operator() (const MatPropVals & lhs , const MatPropVals & rhs) const
        {
           if (lhs.ambient < rhs.ambient) return true;
           if (lhs.ambient > rhs.ambient) return false;
    
           if (lhs.diffuse < rhs.diffuse) return true;
           if (lhs.diffuse > rhs.diffuse) return false;
    
           if (lhs.specular < rhs.specular) return true;
           if (lhs.specular > rhs.specular) return false;
    
           return lhs.emission < rhs.emission;
        }
    };

  5. #5
    Join Date
    Apr 2003
    Location
    kathmandu, nepal
    Posts
    1,570

    Re: std::map containing a struct of four doubles as key

    Thanks a lot.

    Codeguru is faster than even instant messaging
    If there is no love sun won't shine

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