日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CMap在用CString做key类型时,ARG_KEY要选LPCTSTR

發布時間:2025/3/21 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CMap在用CString做key类型时,ARG_KEY要选LPCTSTR 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
CMap在用CString做key類型時,ARG_KEY要選LPCTSTR?
文章來源:http://blog.csdn.net/flyingxu/archive/2005/12/26/562852.aspx

遇到好幾個人說CMap在用CString做key類型時有問題,說用int和DWORD就可以,用CString就不行。因此很多人推薦使用MFC中的CMapStringToPtr之類。

看下面的代碼:
//.h
????CMap<CString, LPCTSTR, int, int> typeMap;
//.cpp
????typeMap.SetAt(_T("ONE"),1);
????typeMap.SetAt(_T("TWO"),2);

????int nValue = 0;
????BOOL ret = typeMap.Lookup(_T("ONE"), nValue);
????ret = typeMap.Lookup(_T("THREE"), nValue);
????ret = typeMap.Lookup(_T("TWO"), nValue);
我的代碼運行的很好,我覺得關鍵是ARG_KEY要選LPCTSTR

原因: ?referencehttp://www.codeproject.com/KB/architecture/cmap_howto.aspx

Introduction

Programmers like me, who learnt?STL::map?before?CMap, always think?CMap?is difficult to use, and always try to useCMap?in the way as a?STL::map. In this article, I will explain about?CMap?and what you should do to use it for your own custom classes. And at the end of this article, I will show an example of how to use?CMap?correctly withCString*?(note, I mean?CString?pointer and not?CString?:>)

CMap Internal

The first thing to be noted is that?CMap?is actually a hash map, and not a tree map (and usually a Red-black tree) asSTL::map. Shown below is the internal structure of a?CMap.

How to declare a CMap

Many people get confused about?CMap's declaration?CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>, why not justCMap<KEY, VALUE>?

In fact, the ultimate data container in?CMap?is?CPair, and the internal of?CPair?is?{KEY, VALUE}. Therefore,?CMapwill really store a?KEY, and not?ARG_KEY. However, if you check with the MFC source code, almost all the internal parameters passing within?CMap?itself is called with?ARG_KEY?and?ARG_VALUE, therefore, using?KEY&?as?ARG_KEYseems always a correct thing, except when:

  • You are using primitive date types like?int,?char, where pass-by-value makes no difference (may be even faster) with pass-by-reference.
  • If you use?CString?as?KEY, you should use?LPCTSTR?as?ARG_KEY?and not?CString&, we will talk more about this later.
  • So what should I do to make CMap work with my ClassX

    Well, as I mentioned earlier,?CMap?is a hash map, a hash map will try to get the "hash value" -- a?UINT?-- from the key, and use that hash value as the index in the hash table (well, actually it is hash value % hash table size). If more then one key have the same hash value, they will be linked in a linked list. Therefore, the first thing you have to do is to provide a hash function.

    CMap?will call a templated function?HashKey()?to do the hashing. The default implementation and specialized version for?LPCSTR?and?LPCWSTR?are listed as follows:

    ?Collapse|?Copy Code // inside <afxtemp.h> template<class ARG_KEY> AFX_INLINE UINT AFXAPI HashKey(ARG_KEY key) {// default identity hash - works for most primitive values return (DWORD)(((DWORD_PTR)key)>>4); }// inside <strcore.cpp> // specialized implementation for LPCWSTR #if _MSC_VER >= 1100 template<> UINT AFXAPI HashKey<LPCWSTR> (LPCWSTR key) #else UINT AFXAPI HashKey(LPCWSTR key) #endif {UINT nHash = 0;while (*key)nHash = (nHash<<5) + nHash + *key++;return nHash; }// specialized implementation for LPCSTR #if _MSC_VER >= 1100 template<> UINT AFXAPI HashKey<LPCSTR> (LPCSTR key) #else UINT AFXAPI HashKey(LPCSTR key) #endif {UINT nHash = 0;while (*key)nHash = (nHash<<5) + nHash + *key++;return nHash; }

    As you can see, the default behavior is to "assume" that the key is a pointer, and convert it to?DWORD, and that's why you will get?"error C2440: 'type cast': cannot convert from 'ClassXXX' to 'DWORD_PTR'"?if you don't provide a specialized?HashKey()?for your?ClassX.

    And because MFC only has specialized implementations for the?LPCSTR?and?LPCWSTR, and not for?CStringA?norCStringW, if you want to use?CString?in?CMap, you have to declare?CMap<CString, LPCTSTR....>.

    OK, now you know how?CMap?calculates the hash value, but since more than one key may have the same hash value,CMap?needs to traverse the whole linked list to find the one with exactly the same key "content", not only with the same "hash value". And when?CMap?does the matching, it will call?CompareElements(), another templated function.

    ?Collapse|?Copy Code // inside <afxtemp.h> // noted: when called from CMap, // TYPE=KEY, ARG_TYPE=ARG_TYPE // and note pElement1 is TYPE*, not TYPE template<class TYPE, class ARG_TYPE> BOOL AFXAPI CompareElements(const TYPE* pElement1, const ARG_TYPE* pElement2) {ASSERT(AfxIsValidAddress(pElement1, sizeof(TYPE), FALSE));ASSERT(AfxIsValidAddress(pElement2, sizeof(ARG_TYPE), FALSE));// for CMap<CString, LPCTSTR...> // we are comparing CString == LPCTSTR return *pElement1 == *pElement2; }

    Therefore, if you want to use?CMap?with your own custom?ClassX, you will have to provide a specialized implementation for?HashKey()?and?CompareElements().

    Example: CMap with CString*

    Provided as an example, below is what you need to do to make?CMap?work with?CString*, and of course, using the string content as the key, and not the address of the pointer.

    ?Collapse|?Copy Code template<> UINT AFXAPI HashKey<CString*> (CString* key) {return (NULL == key) ? 0 : HashKey((LPCTSTR)(*key)); }// I don't know why, but CompareElements can't work with CString* // have to define this typedef CString* LPCString;template<> BOOL AFXAPI CompareElements<LPCString, LPCString> (const LPCString* pElement1, const LPCString* pElement2) {if ( *pElement1 == *pElement2 ) {// true even if pE1==pE2==NULL return true;} else if ( NULL != *pElement1 && NULL != *pElement2 ) {// both are not NULL return **pElement1 == **pElement2;} else {// either one is NULL return false;} }

    And the main program is as simple as:

    ?Collapse|?Copy Code int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) {CMap<CString*, CString*, int, int> map;CString name1 = "Microsoft";CString name2 = "Microsoft";map[&name1] = 100;int x = map[&name2];printf("%s = %d\n", (LPCTSTR)name1, x);*/return 0; } ?Collapse|?Copy Code --------- console output --------- Microsoft = 100

    Please note that the program can compile without error even without the specialized?HashKey()?andCompareElements(), but of course, the output will then be 0, probably not what you want.

    My final note about CMap

  • CMap?is a hash map and?STL::map?is a tree map, there is no meaning comparing the two for performance (it would be like comparing apples and oranges!). But if you will retrieve the keys in sorted order, then you will have to use?STL::map.
  • The design of?HashKey()?is critical to the overall performance. You should provide a?HashKey()?that has a low collision rate (different keys generally would have different hash values)?and?is easy to calculate (not a MD5 of the string, etc..). And we have to note that -- at least for some of the classes -- this is not easy.
  • When using?CMap?(as well as?STL::hash_map), always beware of the hash table size. As quoted in MSDN, "the hash table size should be a prime number. To minimize collisions, the size should be roughly 20 percent larger than the largest anticipated data set". By default,?CMap?hash table size is 17, which should be okay for around 10 keys. You can change the hash table size with?InitHashTable(UINT nHashSize), and can only do so before the first element is added to the map. You can find more prime numbers?here. (And don't mix-up with?CMap(UINT nBlockSize),?nBlockSize?is to acquire more than one?CAssoc?to speed up the creation of a new node.)

  • 總結

    以上是生活随笔為你收集整理的CMap在用CString做key类型时,ARG_KEY要选LPCTSTR的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 国产在线欧美 | 日韩激情视频在线 | 一区二区三区四区久久 | a级片在线播放 | 狂野欧美性猛交xxxx巴西 | 精品黑人一区二区三区国语馆 | 色噜噜狠狠成人中文 | 日韩播放 | 国产后入清纯学生妹 | 精品少妇一区二区三区密爱 | 好屌妞视频这里有精品 | 国产成人看片 | 日韩亚洲欧美精品 | 国内黄色片 | 亚洲午夜无码久久久久 | 美女久久 | 噜噜色成人 | 欧美69精品久久久久久不卡 | 黑帮大佬和我的三百六十五天 | 国产情侣免费视频 | 99热这里只有精品4 精品国产黄色 | 久久免费看少妇高潮v片特黄 | 国产做爰免费观看视频 | 在线观看成人网 | 精品国产精品三级精品av网址 | 老司机一区二区 | 天海翼视频在线观看 | 人与性动交zzzzbbbb | 2021av视频 | 午夜影视免费 | 韩国主播青草55部完整 | 香蕉狠狠爱视频 | 日韩成人免费 | 国产女教师bbwbbwbbw | 国产精品自在线 | 老司机深夜福利影院 | 国产在线观看一区二区三区 | 亚洲一区二区久久 | 青草久久网 | 依依成人综合网 | 黄色片亚洲 | 国产性生活 | 贝利弗山的秘密1985版免费观看 | free性满足hd国产精品久 | 亚洲av永久纯肉无码精品动漫 | 国产成人啪免费观看软件 | 一本之道高清无码视频 | 中文字幕日韩专区 | 西西人体www大胆高清 | 天天色天天射天天操 | 国产h视频在线观看 | 国产11页| 国产91视频在线 | 国产成人精品亚洲 | 日韩国产小视频 | 亚洲国产精品久久 | 日本成人一区 | 不卡的免费av | 免费黄色一级大片 | 成人v片| 一本久久精品一区二区 | 茄子视频色 | 特级西西444www大精品视频免费看 | 操到喷水 | 日韩亚洲欧美中文字幕 | 97视频在线观看免费高清完整版在线观看 | 强制高潮抽搐哭叫求饶h | 91亚洲视频在线 | 伊人亚洲天堂 | 四虎av影院 | 男男全肉变态重口高h | 熟妇人妻久久中文字幕 | 人妻一区二区视频 | 波多野结衣av在线免费观看 | 国产成人综合亚洲 | 最新中文字幕2019 | 黄色福利站 | 自拍偷拍亚洲欧美 | 男人天堂va | 在线播放a | 在线精品播放 | 在线黄色网页 | 九月婷婷 | 亚洲精品中文字幕乱码无线 | 欧美丰满老熟妇aaaa片 | 五月天婷婷色综合 | 91精品国产一区二区三区蜜臀 | 五月婷婷激情综合 | 日日碰| 天天干天天爱天天操 | 国产精品不卡视频 | 国产精品99精品久久免费 | 欧美日韩一级视频 | 欧美在线一二三 | 欧美精品在欧美一区二区少妇 | 韩国三级做爰高潮 | 国产51自产区| 久久精品美女视频 | 国产偷人视频 |