map中使用自定义类指针作为key
2012-05-11 11:58
876 查看
//先上代码
#pragma once
//想用类作为key,必须重载<运算符 或者提供
//想用指针作为key,也是可以的,不过要自己提供仿函数
class CBase
{
public:
explicit CBase(int a);
~CBase(void);
private:
int m_a;
public:
int get() const{return m_a;}
public:
//作为成员函数,只需要传入另外一个对象的引用即可,因为自己已经是个对象了
bool operator < ( const CBase& b ) const
{
return ( this->m_a < b.m_a );
}
};
template<class _Ty>
struct less2
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left->get() > _Right->get();
}
};
#include "Base.h"
CBase::CBase(int a)
{
m_a = a;
}
CBase::~CBase(void)
{
}
#include "Base.h"
#include <map>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
map<CBase*,int,less2<CBase*>> b_i_map;
CBase* a = new CBase(1);
CBase* b = new CBase(2);
CBase* c = new CBase(3);
CBase* d = new CBase(4);
b_i_map.insert(make_pair<CBase*,int>(a,1));
b_i_map.insert(make_pair<CBase*,int>(b,2));
b_i_map.insert(make_pair<CBase*,int>(c,3));
b_i_map.insert(make_pair<CBase*,int>(d,4));
for ( map<CBase*,int,less2<CBase*>>::iterator it = b_i_map.begin();
it != b_i_map.end(); it++ )
{
cout << (*it).first->get() <<" " <<(*it).second <<endl;
}
cout <<endl;
for ( map<CBase*,int,less2<CBase*>>::iterator it = b_i_map.begin();
it != b_i_map.end(); it++ )
{
delete (*it).first;
}
b_i_map.clear();
return 0;
}
这时如果想用自定义类作为key的必须要实现 < 运算符重载,
如果是使用指针作为key,必须提供一个仿函数模板,在声明map类型时引入。
下面举例说明一下,自定义结构作为map的key时重载 <运算符的实现,可以参考下
typedef struct tagUniqueBoard
{
int m_uShelf;
int m_uSlot;
std::string m_strBoardName;
std::string m_strNEid;
bool operator < (const tagUniqueBoard& b) const
{
if( m_uShelf != b.m_uShelf)
{
return m_uShelf < b.m_uShelf;
}
else if( m_uSlot != b.m_uSlot)
{
return m_uSlot < b.m_uSlot;
}
else if( m_strBoardName != b.m_strBoardName)
{
return m_strBoardName < b.m_strBoardName;
}
else if( m_strNEid != b.m_strNEid)
{
return m_strNEid < b.m_strNEid;
}
return false;
}
}UniqueBoard;
#pragma once
//想用类作为key,必须重载<运算符 或者提供
//想用指针作为key,也是可以的,不过要自己提供仿函数
class CBase
{
public:
explicit CBase(int a);
~CBase(void);
private:
int m_a;
public:
int get() const{return m_a;}
public:
//作为成员函数,只需要传入另外一个对象的引用即可,因为自己已经是个对象了
bool operator < ( const CBase& b ) const
{
return ( this->m_a < b.m_a );
}
};
template<class _Ty>
struct less2
{
bool operator()(const _Ty& _Left, const _Ty& _Right) const
{
return _Left->get() > _Right->get();
}
};
#include "Base.h"
CBase::CBase(int a)
{
m_a = a;
}
CBase::~CBase(void)
{
}
#include "Base.h"
#include <map>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
map<CBase*,int,less2<CBase*>> b_i_map;
CBase* a = new CBase(1);
CBase* b = new CBase(2);
CBase* c = new CBase(3);
CBase* d = new CBase(4);
b_i_map.insert(make_pair<CBase*,int>(a,1));
b_i_map.insert(make_pair<CBase*,int>(b,2));
b_i_map.insert(make_pair<CBase*,int>(c,3));
b_i_map.insert(make_pair<CBase*,int>(d,4));
for ( map<CBase*,int,less2<CBase*>>::iterator it = b_i_map.begin();
it != b_i_map.end(); it++ )
{
cout << (*it).first->get() <<" " <<(*it).second <<endl;
}
cout <<endl;
for ( map<CBase*,int,less2<CBase*>>::iterator it = b_i_map.begin();
it != b_i_map.end(); it++ )
{
delete (*it).first;
}
b_i_map.clear();
return 0;
}
这时如果想用自定义类作为key的必须要实现 < 运算符重载,
如果是使用指针作为key,必须提供一个仿函数模板,在声明map类型时引入。
下面举例说明一下,自定义结构作为map的key时重载 <运算符的实现,可以参考下
typedef struct tagUniqueBoard
{
int m_uShelf;
int m_uSlot;
std::string m_strBoardName;
std::string m_strNEid;
bool operator < (const tagUniqueBoard& b) const
{
if( m_uShelf != b.m_uShelf)
{
return m_uShelf < b.m_uShelf;
}
else if( m_uSlot != b.m_uSlot)
{
return m_uSlot < b.m_uSlot;
}
else if( m_strBoardName != b.m_strBoardName)
{
return m_strBoardName < b.m_strBoardName;
}
else if( m_strNEid != b.m_strNEid)
{
return m_strNEid < b.m_strNEid;
}
return false;
}
}UniqueBoard;
相关文章推荐
- map中使用自定义类指针作为key
- 历年世界杯冠军队伍如下,使用map存储这些队伍,使用年份作为key,冠军队伍名称作为value。 完成下面的功能: 1) 输入一个年份,如果当年有世界杯比赛,则输出获奖队伍,如果没有则输出“当年没有举
- 使用类/结构体作为boost::unordered_map中的key时需要实现hash_value函数
- 使用map来进行票数统计工作,循环输入多个人名,作为key存储到map中,对应的value就是该人获得的票数(即重复输入的次数),当重复输入时,需要对相应的数据进行修改。最红输quit结束循环,打印
- java中map使用非系统类作为key
- linux下C++ STL hash_map的使用以及使用char *型变量作为Key值的一大“坑”
- 有关STL中map的key是指针的情况分析和其他map使用注意点
- 【JavaDemo】使用key遍历含自定义类的Map集合
- 使用用户自定义类型作为map的key
- java中map使用非系统类作为key
- Map使用非系统类作为key,要覆写equals和hashCode方法
- Map中使用数组作为key的用法
- Java 在 Map 中使用复杂数据类型作为 Key
- map中无法使用TCHAR*作为key的解决方法
- 【LibUIDK系列文章】使用字符串作为map的Key
- python中使用自定义类实例作为字典的key
- groovy 使用变量值作为 map 的key--需要注意的
- 如何取出Map中的key和value--Map.Entry 类使用简介
- 谨慎使用String作为HashMap的Key
- 使用指针作为函数参数的注意事项