multiple key map

¸Ê¿¡¼­ Å°·Î °Ë»ö½Ã, µÎ °³ ÀÌ»óÀÇ Å°·Î °Ë»öÇÏ´Â °æ¿ì¿¡´Â multiple key¸¦ ÀÌ¿ëÇϰųª, Å° Á¶ÇÕ¿¡ ÀÇÇØ  64ºñÆ® Å©±â·Î ¸¸µé¾î¼­ »ç¿ëÇÏ´Â ¹æ¹ýÀÌ ÀÖ´Ù.

64ºñÆ®·Î ¸¸µé¾î¼­ »ç¿ëÇÏ´Â °æ¿ì´Â ¸ðµÎ ¾Ë°í ÀÖÀ»°ÍÀÌ´Ù.

ÀÌ Àå¿¡¼­´Â 2°³ ÀÌ»óÀÇ Å°·Î ¸ÊÀ» °Ë»öÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ­ ¾Ë¾Æ º¸°íÀÚ ÇÑ´Ù.

Map¿¡¼­ Insert³ª Find½Ã¿¡´Â "operator <" ¿¬»êÀÚ¸¦ ²À ÀçÁ¤ÀÇÇØ¾ß ÇÑ´Ù.

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

¸Ê¿¡¼­ »ç¿ëÇÏ°í ÀÖ´Â lessÀÇ °æ¿ì µÎ  operand¸¦ ¸ðµÎ const·Î ¹Þ°í Àֱ⠶§¹®¿¡ const ¸â¹ö ÇÔ¼ö·Î ±¸ÇöÇØÁÖ¾î¾ß ÇÑ´Ù.

template<class  _Ty>

struct  less : binary_function<_Ty, _Ty, bool>

{

        bool  operator()(const  _Ty& _X, const  _Ty& _Y) const

        {return (_X < _Y); }

};

< ¼Ò½º >

 

class  TwoKey

{

public:

    long  key1;

    int   key2;

 

    TwoKey(long  k1, int  k2)

      : key1(k1), key2(k2)

    {

    } 

 

    bool  operator<(const  TwoKey &right) const

    {

        if ( key1 == right.key1 )

        {

            return  key2 < right.key2;

        }

        else

        {

            return  key1 < right.key1;

        }

    }   

};

 

struct  twokey_less : public  std::binary_function<TwoKey, TwoKey, bool>

{

    bool  operator()(const  TwoKey &mkey1, const  TwoKey &mkey2 ) const

    {

        return  mkey1.operator<(mkey2); // mkey1 < mkey2;

    }

};

< TwoKey »ç¿ë ¿¹Á¦ >

twokey_less¸¦ ±¸Çö ÇÏÁö ¾Ê´õ¶óµµ ½ÇÇà¿¡´Â ÁöÀåÀÌ ¾ø´Ù.
operator<  ¸¸ ±¸ÇöÇصµ °Ë»ö ÇÒ ¶§ ¹®Á¦°¡ ¾ø´Ù. ±»ÀÌ twokey_less¸¦ ±¸Çö ÇÒ ÇÊ¿ä°¡ ¾ø´Ù.

    std::map<TwoKey, int, twokey_less> twomap;

    TwoKey t1( 1, 1 );

    TwoKey t2( 1, 2 );

    TwoKey t3( 1, 3 );

    TwoKey t4( 1, 4 );

    TwoKey t5( 2, 3 );

    TwoKey t6( 2, 4 );

 

    twomap.insert( std::make_pair( t1, 1 ) );

    twomap.insert( std::make_pair( t2, 2 ) );

    twomap.insert( std::make_pair( t3, 3 ) );

    twomap.insert( std::make_pair( t4, 4 ) );

    twomap.insert( std::make_pair( t5, 5 ) );

    twomap.insert( std::make_pair( t6, 6 ) );

 

    std::map<TwoKey, int, twokey_less>::iterator tIter = twomap.find( t4 );

    if( tIter != twomap.end() )

        std::cout <<  "twokey : t4(1, 4)" << "  value :" << tIter->second << std::endl;

 

    tIter = twomap.find( t6 );

    if( tIter != twomap.end() )

        std::cout <<  "twokey : t6(2, 4)" << "  value :" << tIter->second << std::endl;

map_multikey.cpp ¼Ò½º¸¦ º¸¸é 4°³ Å°·Î °Ë»öÇÏ´Â Multikey ¿¹Á¦µµ º¼¼ö ÀÖ´Ù.

¼Ò½º : map_multikey.cpp

°ü·Ã ¸µÅ©:
map_twoKey.html
map_twoKey1.html