CMap的成员函数SetAt和Lookup用法实例

遇到好多次这玩意了,老也理解不了,今天忽然觉得有点明白了,选写下一点,等以后再深刻理解了再补充吧。觉得这个例子不错,简单也容易理解一些。

#include   <afxwin.h>   
#include   <afxtempl.h>   
void   main()   
{   
AfxWinInit(::GetModuleHandle(NULL),   NULL,   ::GetCommandLine(),   0);   
CMap<int,   int,   CString,   CString>   m_cMap; 
CMapStringToString m_shi;
m_cMap.SetAt(9923033,     "张三");   
m_cMap.SetAt(9826033,     "张A");   
m_cMap.SetAt(9923063,     "张B");   
m_cMap.SetAt(9923093,     "张C");   
m_shi.SetAt("wang","王");
m_shi.SetAt("wei","伟");
m_shi.SetAt("cai","才");
CString   strName; 
int i=m_cMap.Lookup(0,   strName);   
AfxMessageBox(strName);
Sleep(500);
i=m_cMap.Lookup(9826033,   strName); 
AfxMessageBox(strName);

Sleep(500);
i=m_shi.Lookup("cai",strName);
AfxMessageBox(strName);
}


MSDN看不太明白,自我理解是:Lookup的第一项必须是SetAt第一项中的一个才会返回有效值。找到的话函数的返回值在我的机子上老返回的是1,用Lookup的第二项来得到一个自己需要的数值。可能表述有问题,请见谅!

这个可能会提示链接错误,可是参照本空间的   《 LNK1120: 2 unresolved externals   》这篇文章。

// this file must be compiled with the /GX and /MT options:
//      cl /GX /MT thisfile.cpp

#include <afx.h>
#include <afxdb.h>
#include <iostream.h>

int main()
{
   // try to initialize MFC

   if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
   {
      cerr << "MFC failed to initialize!" << endl;
      return 1;
   }

   // try to connect to an ODBC database that doesn't exist
   // (this wouldn't work at all without initializing MFC)

   CDatabase db;
   try
   {
      db.Open("This Databsae Doesn't Exist");

      // we shouldn't realistically get here

      cout << "Successful!" << endl;
      cout << "Closing ... ";
      db.Close();
      cout << "Closed!" << endl;
   }
   catch (CDBException* pEx)
   {
      // we got an exception! print an error message
      // (this wouldn't work without initializing MFC)

      char sz[1024];

      cout << "Error: ";
      if (pEx->GetErrorMessage(sz, 1024))
         cout << sz;
      else
         cout << "No error message was available";
      cout << endl;

      pEx->Delete();
      return 1;
   }

   return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值