64,654
社区成员
发帖
与我相关
我的任务
分享
struct EntryInfo
{
EntryInfo()
{
memset( this, 0, sizeof(EntryInfo) );
}
char achEntryName[16];
byte byEntryType;
byte byAddrType;
WORD wDialBitRate;
char achUIName[16];
BOOL operator == ( const EntryInfo &tEntry ) const
{
if ( byEntryType != tEntry.byEntryType )
return FALSE;
if( byAddrType != tEntry.byAddrType )
return FALSE;
if ( strcmp( achEntryName, tEntry.achEntryName ) != 0 )
return FALSE;
return TRUE;
}
BOOL operator < ( const EntryInfo &tEntry ) const
{
if( this->byEntryType > tEntry.byEntryType )
{
return FALSE;
}
else if(this->byEntryType == tEntry.byEntryType)
{
if( this-> byAddrType > tEntry.byAddrType )
{
return FALSE;
}
else if( strcmp(this->achEntryName, tEntry.achEntryName) >0 )
{
if( strcmp( this->achUIName, tEntry.achUIName ) > 0 )
{
return TRUE;
}
else
{
return FALSE;
}
}
else
{
return TRUE;
}
}
else
{
return TRUE;
}
}
};
typedef map<EntryInfo, int> defMap;
defMap g_map;
void CStl_mapDlg::OnButton2()
{
EntryInfo t1;
strcpy(t1.achEntryName, "sq");
strcpy(t1.achUIName, "sq_UI");
t1.byAddrType = 1;
t1.byEntryType =1;
t1.wDialBitRate = 1;
g_map.insert( defMap::value_type(t1, 1));
EntryInfo t2;
strcpy(t2.achEntryName, "sq2");
strcpy(t2.achUIName, "sq_UI2");
t2.byAddrType = 2;
t2.byEntryType =2;
t2.wDialBitRate = 2;
g_map.insert( defMap::value_type(t2, 2));
EntryInfo t3;
strcpy(t3.achEntryName, "sq3");
strcpy(t3.achUIName, "sq_UI3");
t3.byAddrType = 3;
t3.byEntryType =3;
t3.wDialBitRate = 3;
g_map.insert( defMap::value_type(t3, 3));
}
void CStl_mapDlg::OnButton3()
{
EntryInfo t3;
strcpy(t3.achEntryName, "sq3");
strcpy(t3.achUIName, "sq_UI3");
t3.byAddrType = 3;
t3.byEntryType =3;
t3.wDialBitRate = 3;
int x = g_map[t3];//饭后一直是0
}
bool operator < ( const EntryInfo &tEntry ) const
{
if( this->byEntryType > tEntry.byEntryType )
{
return FALSE;
}
else if(this->byEntryType == tEntry.byEntryType)
{
if( this-> byAddrType > tEntry.byAddrType )
{
return FALSE;
}
// else if( strcmp(this->achEntryName, tEntry.achEntryName) >0 )
// {
// if( strcmp( this->achUIName, tEntry.achUIName ) > 0 )
// {
// return TRUE;
// }
// else
// {
// return FALSE;
// }
//
// }
else
{
return TRUE;
}
}
else
{
return TRUE;
}
return FALSE;
}
#include <iostream>
#include <map>
using namespace std;
typedef struct S
{
public:
S(int i):m(i)
{
}
S(const S &obj):m(obj.m)
{
}
bool operator <(const S &obj) const //因为less类里的operator()有const
{
return m < obj.m;
}
~S()
{
}
private:
int m;
}Struct;
int main()
{
map<Struct,int> mp; //less<>默认使用对象的<运算符,所以重载对象的<是OK的
//只需要重载<,原因:
//平衡二叉树:
/*
less<Struct> cmp;
if(cmp(a->data,b->data)) a是待插入结点,b是当前结点,data是结点里Struct对象
{
go to left ;
}
// 已知a>=b,需要判断b<a还是b==a
else if(cmp(b->data,a->data)) //如果b<a成立,那么走右边
{
go to right;
}
else //b==a,由于是map不是multi_map,报错
{
throw ....
}
*/
Struct a(1);
Struct b(3);
Struct c(2);
mp.insert(make_pair(a,1));
mp.insert(make_pair(b,1));
mp.insert(make_pair(c,1));
return 0;
}