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

Threaded View

  1. #1
    Join Date
    Feb 2012
    Posts
    19

    std::map and comparison function

    EDIT: SOLVED!

    Hi

    once again i need your help.

    I have a struct representing a poker hand category.

    Code:
    typedef struct{
      int rank1;
      int rank2;
      bool isSuited;
    }HAND169;
    I want to precalculate the equity of every category against every category. Hence i want to make a table:

    Code:
    typedef map<HAND169, map<HAND169, pair<double,double>>> LOOKUP1ON1;
    Now, if i want to work with this map, the compiler desires a comparison function from me. But i really have no clue how to implement it. I dont understand the example at http://www.cplusplus.com/reference/stl/map/map/ and the numerous articles in the web did not help me. Can you please help me with it?

    EDIT: SOLUTION

    Code:
    typedef struct{
      int rank1;
      int rank2;
      bool isSuited;
    }HAND169;
    
    struct classcomp {
      bool operator() (const HAND169 &h1, const HAND169 &h2) const
      {
        if (h1.rank1 < h2.rank1)
          return true;
        else if (h1.rank1 == h2.rank1 && h1.rank2 < h2.rank2)
          return true;
        else if (h1.rank1 == h2.rank1 && h1.rank2 == h2.rank2 && h1.isSuited < h2.isSuited)
          return true;
        return false;
      }
    };
    
    typedef map<HAND169, map<HAND169, pair<double,double>,classcomp>, classcomp> LOOKUP1ON1 ;
    Forgot the inner map. Anyway thanks for reading ;-D
    Last edited by ImNotaBot; April 17th, 2012 at 09:07 AM.

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