早上好,我被困在正确的方式使用地图。C++地图比较
形势
具有独特的ID和其他两位代码的数据库表
ID (long) | Type (long) | Name (string)
,填补了地图正确,我这样定义它:
map<long, MyObject>
哪里关键是我的ID和对象拥有所有的东西。地图工作正常,我加载所有的行,我很容易在里面导航。
烦恼
麻烦来当我需要使用一个标准这不是关键,但该行进行排序:
- 类型
- 名称
环顾互联网我发现我应该:
- 为MyObject定义运算符<或...
- 为我的映射定义另一种类型的比较器。
我做了第1步,但没有成功(它从未被调用过)。我正在努力做到这一点,但成效不大。 我会粘贴一些代码,以帮助:
class CSitoWebRigaVariante
{
public:
bool m_bSriDelete;
bool m_bSriVisibile;
long m_lSriId;
long m_lSriIdTipol;
long m_lSriCodGes;
CString m_strSriCodMat;
public:
CSitoWebRigaVariante(void);
CSitoWebRigaVariante(const CSitoWebRigaVariante& cRiga);
~CSitoWebRigaVariante(void);
bool operator<(const CSitoWebRigaVariante& cRiga);
void operator=(const CSitoWebRigaVariante& cRiga);
void Azzera(void);
static void CaricaDaMDB(CDB* pDB, long lIdVM, map<long, CSitoWebRigaVariante>& cRighe);
};
typedef map<long, CSitoWebRigaVariante> CSWRighe;
///> Static method to fill a map.
void CSitoWebRigaVariante::CaricaDaMDB(CADODatabase* pDB, long lIdVM, map<long, CSitoWebRigaVariante>& cRighe)
{
BOOL bValRit;
CRecordset* pRS;
CSitoWebRigaVariante riga;
CString strInt;
pRS = new CADORecordset(pDB);
strInt.Format(_T("SELECT * FROM SITOWEB_RIVARMAT WHERE sri_idvarmat = %ld;"), lIdVM);
cRighe.clear();
if (pRS->Open(strInt, CADORecordset::openQuery) == TRUE && pRS->GetRecordCount() > 0)
{
while (pRS->IsEOF() == FALSE)
{
bValRit = pRS->GetFieldValue(_T("sri_id"), riga.m_lSriId);
bValRit &= pRS->GetFieldValue(_T("sri_idtipol"), riga.m_lSriIdTipol);
bValRit &= pRS->GetFieldValue(_T("sri_codges"), riga.m_lSriCodGes);
bValRit &= pRS->GetFieldValue(_T("sri_codmat"), riga.m_strSriCodMat);
bValRit &= pRS->GetFieldValue(_T("sri_delete"), riga.m_bSriDelete);
bValRit &= pRS->GetFieldValue(_T("sri_visibile"), riga.m_bSriVisibile);
cRighe.insert(pair<long, CSitoWebRigaVariante>(riga.m_lSriCodGes, riga));
pRS->MoveNext();
}
}
pRS->Close();
delete pRS;
}
我使用Visual Studio 2010,MFC。 任何帮助表示赞赏。
我还是不明白:ID是唯一的,Type和Name不是,为什么使用多个索引?我不应该重新定义排序标准吗,或者我错过了什么? – IssamTP
@IssamTP:详细说明了答案 –
基本上我应该定义类似于:class CKey {long id;长型;字符串名称;运算符<(CKey one,CKey two){/ * mysort criteria * /}},即使我在访问成员时遇到问题。非常感谢。 – IssamTP