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