CMap 语录

1.template<class KEY, class ARG_KEY, class VALUE, class ARG_VALUE>

它继承了CArray以及CList的参数风格,将传入类型与返回类型分开,其中带有ARG_前缀的是传入类型,另外一个则是返回类型,这已经是我们不止一次的抱怨为什么要将类型参数分开了,其实实践证明,它们的确可以被合并起来,不过参数传入的灵活性将会受到部分的影响。

http://hi.baidu.com/idealsoft/blog/item/69687064c504b4f5f73654b3.html

2.之所以KEY可以使用CString而ARG_KEY却用的是LPCTSTR,那是因为CString重载了operator==(const char*)这个判等操作符,当CMapHash表中获得KEY之后,它会将ARG_KEY与KEY直接相比较。真正存于CMap内部的是KEY,也就是CString。这也就是为什么,我们经常会看到CMap被实化成CMap<CString, LPCTSTR/*相当于const char*,非Unicode情况下*/, CStringCString&>这样的一个四不像实化类的原因

http://hi.baidu.com/idealsoft/blog/item/69687064c504b4f5f73654b3.html

Example:

#include <Afxtempl.h>
#include <iostream>
using namespace std;
class Point
{
public:
    Point()
    {
        m_x = 0;
        m_y = 0;
    }
 
    Point(int x, int y)
    {
        m_x = x;
        m_y = y;
    }
public:
    int m_x;
    int m_y;
};

typedef CMap<const char*, const char*, Point, Point&>     CMapPnt; //ÇëÔÚʹÓÃ֮ǰ¶¨Òå

int main()
{
    Point elem1(1, 100), elem2(2, 200), elem3(3, 300), point;
    CMapPnt mp;

    // insert 3 elements into map£¬          #1
    mp.SetAt("1st", elem1);
    mp.SetAt("2nd", elem2);
    mp.SetAt("3th", elem3);

    // search a point named "2nd" from map                  #2
    mp.Lookup("2nd", point);
    printf("2nd: m_x: %d, m_y: %d/n", point.m_x, point.m_y);

 // insert a new pair into map      #3
 Point elem4(4, 400);
 mp["4th"] = elem4;
 cout<<"count: "<<mp.GetCount()<<endl;
 
 // traverse the entire map                    #4
 size_t index = 0;
 const char* pszKey;
 POSITION ps = mp.GetStartPosition();

 while( ps )
 {  
  mp.GetNextAssoc(ps, pszKey, point);
  printf("index: %d, m_x: %d, m_y: %d/n", ++index, point.m_x, point.m_y);
 }
 
 return 0;
}

 

你可能感兴趣的:(CMap 语录)