-----------------------------------------------------------
// 自定义类CMap<char*, char*, int, int> m_IdtoInfo; // .h中// .cpp中
void CDataCtrl::SetInfo(char* caId, int info)
{
  char* pTempId = new char[22];
  ZeroMemory(pTempId,22);
  memcpy(pTempId,caId,22);
  ......
  m_IdtoInfo.SetAt(pTempId,...);
  ......
}BOOL CDataCtrl::GetInfo(char* caId, int info)
{
  char* pTempId = (char*)caId;
  ......
  if (m_IdtoInfo.LookUp(pTempId,...) == FALSE)
     return FALSE;
  return TRUE;
  ......
}--------------------------------------------------------------------------------.....................
// 应用
CDataCtrl m_Data;
m_Data.SetInfo("13934567890",...);
m_Data.GetInfo("13934567890",...);       // 为什么一直返回FALSE,而得不到对应的int值?CMap<char*,char*,......>应该怎么写?

解决方案 »

  1.   

    long的最大长度才是2147483647
      

  2.   

    char* pTempId = new char[22];?
    pTempId!=caId,所以Lookup不成功。试试CMap<CString,CString,int,int>
      

  3.   

    参考:
    MAP的声明: 
    加上<afxtempl.h> 
    typedef CMap<DWORD,DWORD,CMyStruct*,CMyStruct*&> CMapDWordToMyStruct; 
    以后直接用CMapDWordToMyStruct进行声明,方便不少。 MAP的初始化: 
    变量定义在Doc中,在OnNewDocument()中进行第一次的初始化。 
    CMyStruct* pMyStruct2 = new CMyStruct(); 
    pMyStruct2->m_int = 2468; 
    pMyStruct2->m_float = 24.68f; 
    pMyStruct2->m_str.LoadString(IDS_INITIAL_STRING); 
    m_mapDWordToMyStruct[100] = pMyStruct2;//是这样插入数据的。 视图启动的时候做这些事情: 
    DWORD dwKey; 
    CMyStruct* pMyStruct; 
    m_ctlList.ResetContent(); 
    CMapDWordToMyStruct& map = GetDocument()->m_mapDWordToMyStruct; 
    POSITION pos = map.GetStartPosition(); 
    while (pos != NULL) 

    map.GetNextAssoc(pos, dwKey, pMyStruct); 
    AddMapEntryToListBox(dwKey, pMyStruct); 

                   //以上代码遍历读取MAP的数据并送到控件中进行显示。 添加和替换操作: 
    // Add or replace entry in the listbox 
    int nSel = FindKeyInListBox(m_dwKey);//从控件中得到Key 
    if (nSel != LB_ERR) 
    m_ctlList.DeleteString(nSel);//如果存在在控件中删除记录 
    CMyStruct* pMyStruct = ConstructMyStructFromView();//从控件数据新建结构 
    AddMapEntryToListBox(m_dwKey, pMyStruct);//加入到列表控件 // Add or update association in the CMap 
    CMyStruct* pTemp; 
    if( GetDocument()->m_mapDWordToMyStruct.Lookup( m_dwKey, pTemp ) ) 
    delete pTemp; // delete old value//查找目标,如果存在则删除,很方便的操作,不是吗? 
    GetDocument()->m_mapDWordToMyStruct[m_dwKey] = pMyStruct;//添加记录 全部删除操作: 
    CCollectDoc* pDoc = GetDocument(); 
    POSITION pos = GetDocument()->m_mapDWordToMyStruct.GetStartPosition(); 
    while (pos != NULL) 

    DWORD dwKey; 
    CMyStruct* pMyStruct; 
    pDoc->m_mapDWordToMyStruct.GetNextAssoc(pos, dwKey, pMyStruct); 
    delete pMyStruct; 

    pDoc->m_mapDWordToMyStruct.RemoveAll(); 
    m_ctlList.ResetContent(); 删除操作: 
    if (UpdateData() != TRUE) 
    return; CMyStruct* pMyStruct; 
    CMapDWordToMyStruct& map = GetDocument()->m_mapDWordToMyStruct; 
    if (map.Lookup(m_dwKey, pMyStruct) != TRUE) 

    AfxMessageBox(IDS_KEY_NOT_FOUND); 
    return; 
    } if (m_int != pMyStruct->m_int 
    || m_float != pMyStruct->m_float 
    || m_str != pMyStruct->m_str) 

    UpdateViewFromMyStruct(pMyStruct); 
    AfxMessageBox(IDS_KEY_NOT_FOUND_CHOOSE_REMOVE_AGAIN); 
    return; 
    } // Remove assocation from the listbox 
    int nSel = FindKeyInListBox(m_dwKey); 
    ASSERT(nSel != LB_ERR); 
    m_ctlList.DeleteString(nSel); // Remove the association from the CMap 
    map.RemoveKey(m_dwKey); // Delete CMyStruct 
    delete pMyStruct; 查找操作: 
    if (UpdateData() != TRUE) 
    return; CMyStruct* pMyStruct; 
    if (GetDocument()->m_mapDWordToMyStruct.Lookup(m_dwKey, pMyStruct) != TRUE) 

    AfxMessageBox(IDS_KEY_NOT_FOUND); 
    return; 
    } UpdateViewFromMyStruct(pMyStruct); 这里主要是CMap在文档视图中的用法,可以看出,微软的代码写的非常严密,安全检查意识很好,函数的封装比较得体。
      

  4.   

    用CMap<CString,LPCTSTR,int,int>
    或CMapStringToString