Map - µÎ °³ÀÇ Å°·Î ã±â

STL ¸Ê¿¡¼­ 2°³ÀÇ Å°·Î °Ë»öÇÏ´Â ¹æ¹ýÀ» ¾Ë¾Æº¸ÀÚ.

MapÀ̳ª Set¿¡¼­ ±âº» µ¥ÀÌÅÍ ÇüÀÌ ¾Æ´Ñ °æ¿ì Insert³ª Find½Ã¿¡´Â "operator <" ¿¬»êÀÚ¸¦ ²À ÀçÁ¤ÀÇÇØ¾ß ÇÑ´Ù.

"opertator <" ¿¬»êÀÚ¸¦ Á¤ÀÇ ÇÒ ¶§, µÎ °³ÀÇ const°¡ µé¾î°£´Ù. »©¸ÔÁö ¸»µµ·Ï ÁÖÀÇÇÏÀÚ !!!
a, b Å©±â¸¦ ºñ±³ÇÒ ¶§, °°°Å³ª Å©¸é false¸¦ ¹ÝȯÇÑ´Ù.

class  Key

{

public:

    int  a, b;

    Key( int  _a, int  _b )

    {

        a = _a;

        b = _b;

    }

 

    bool  operator<( Key const  &rValue ) const

    {

        if(  a < rValue.a )

            return  true;

        else  if( a == rValue.a )

        {

            return  ( b < rValue.b );

        }

 

        return  false;

    }

};

¾Æ·¡´Â Key Ŭ·¡½º¸¦ »ç¿ëÇÏ´Â ¹æ¹ý¿¡ ´ëÇÑ ¿¹Á¦ÀÌ´Ù.
Key °ªÀÌ 4, 1 À̹ǷΠsecond °ªÀº 3ÀÌ´Ù.

typedef  std::map< Key, int > MapTest;

 

void  main()

{

    MapTest map;

 

    map.insert( std::make_pair( Key( 5, 1 ), 5 ) );

    map.insert( std::make_pair( Key( 4, 0 ), 2 ) );

    map.insert( std::make_pair( Key( 3, 1 ), 1 ) );

    map.insert( std::make_pair( Key( 4, 1 ), 3 ) );

    map.insert( std::make_pair( Key( 4, 2 ), 4 ) );

 

    MapTest::iterator iter = map.find( Key( 4, 1 ) );

    if( iter != map.end() )

    {

        printf( "Å×½ºÆ® Å° °ª = %d\n", iter->second );

    }

 

    fflush(stdout);

    _getch();

}

ÇÁ·ÎÁ§Æ® : TwoKeyMap.zip