结构体作为map的key或放入set中,需要重载<运算符
2011-11-25 14:29
411 查看
结构体作为map的key或放入set中,需要重载<运算符,如下:
typedef struct tagRoadKey
{
int m_i32Type;
int m_i32Scale;
bool operator <(const tagRoadKey& other) const // 注意是const函数!!
{
if (m_i32Type != other.m_i32Type) // 类型按升序排序
{
return (m_i32Type < other.m_i32Type);
}
else // 如果类型相同,按比例尺升序排序
{
return (m_i32Scale < other.m_i32Scale);
}
}
} RoadKey;
也可以重载>运算符,示例如下:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Array
{
private:
int m_i32Num1;
int m_i32Num2;
public:
Array(int i32Num1, int i32Num2);
bool operator >(const Array& other) const;
};
Array::Array(int i32Num1, int i32Num2)
{
m_i32Num1 = i32Num1;
m_i32Num2 = i32Num2;
}
bool Array::operator >(const Array& other) const
{
if (m_i32Num1 > other.m_i32Num1)
{
return true;
}
else
{
return false;
}
}
// 此结构体作为map的value
struct TInfo
{
int m_i32Num1;
int m_i32Num2;
};
int main(int argc, char* argv[])
{
map<Array, TInfo, greater<Array> > stMap;
TInfo stInfo1 = { 1, 1};
stMap.insert(pair<Array, TInfo>(Array(1, 2), stInfo1));
TInfo stInfo2 = { 2, 1, 1 };
stMap.insert(pair<Array, TInfo>(Array(2, 2), stInfo2));
TInfo stInfo3 = { 3, 1, 1 };
stMap.insert(pair<Array, TInfo>(Array(3, 2), stInfo3));
for (map<Array, TInfo, greater<Array> >::iterator it = stMap.begin(); it != stMap.end(); ++it)
{
cout << it->second.m_i32Num1 << endl;
}
return 0;
}
说明:
map缺省是用less<Key>作为比较器,所以它要求作为Key的类要重载“<”操作符,没有重载“<”操作符,而是重载了“>”操作符就会报错。
反之,也可以显式地用greater<Key>作为比较器,此时就必要重载Key类中的“>”操作符了。
附:stl中map和set的声明,二者比较像,底层都是用红黑树实现的
template < class Key, class Compare = less<Key>,
class Allocator = allocator<Key> > class set;
template < class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > > class map;
template < class Key, class Compare = less<Key>,
class Allocator = allocator<Key> > class multiset;
template < class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > > class multimap;
从上面的声明可以看出,也可以定义一个函数对象Compare,声明map或set类型时传进入,如:
struct TTimeCompare
{
bool operator ()(const CTimerEvent* po1, const CTimerEvent* po2)const
{
return (po1->m_oNextTick < po2->m_oNextTick);
}
};
typedef multiset<CTimerEvent*, TTimeCompare> TEventSet;
struct ltstr // less than
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
set<const char*, ltstr> stSet; // set<Key, Compare, Alloc>
map<const char*, int, ltstr> stMap; // map<Key, Data, Compare, Alloc>
struct eqstr // equal
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
hash_map<const char*, int, hash<const char*>, eqstr> stHashMap; // hash_map<Key, Data, HashFcn, EqualKey, Alloc>
// 自定义hash函数
namespace std
{
template<>
struct hash<KEY_TYPE>
{
size_t operator()(const KEY_TYPE& key) const
{
//return key.Hash();
}
};
}
typedef struct tagRoadKey
{
int m_i32Type;
int m_i32Scale;
bool operator <(const tagRoadKey& other) const // 注意是const函数!!
{
if (m_i32Type != other.m_i32Type) // 类型按升序排序
{
return (m_i32Type < other.m_i32Type);
}
else // 如果类型相同,按比例尺升序排序
{
return (m_i32Scale < other.m_i32Scale);
}
}
} RoadKey;
也可以重载>运算符,示例如下:
#include <iostream>
#include <string>
#include <map>
using namespace std;
class Array
{
private:
int m_i32Num1;
int m_i32Num2;
public:
Array(int i32Num1, int i32Num2);
bool operator >(const Array& other) const;
};
Array::Array(int i32Num1, int i32Num2)
{
m_i32Num1 = i32Num1;
m_i32Num2 = i32Num2;
}
bool Array::operator >(const Array& other) const
{
if (m_i32Num1 > other.m_i32Num1)
{
return true;
}
else
{
return false;
}
}
// 此结构体作为map的value
struct TInfo
{
int m_i32Num1;
int m_i32Num2;
};
int main(int argc, char* argv[])
{
map<Array, TInfo, greater<Array> > stMap;
TInfo stInfo1 = { 1, 1};
stMap.insert(pair<Array, TInfo>(Array(1, 2), stInfo1));
TInfo stInfo2 = { 2, 1, 1 };
stMap.insert(pair<Array, TInfo>(Array(2, 2), stInfo2));
TInfo stInfo3 = { 3, 1, 1 };
stMap.insert(pair<Array, TInfo>(Array(3, 2), stInfo3));
for (map<Array, TInfo, greater<Array> >::iterator it = stMap.begin(); it != stMap.end(); ++it)
{
cout << it->second.m_i32Num1 << endl;
}
return 0;
}
说明:
map缺省是用less<Key>作为比较器,所以它要求作为Key的类要重载“<”操作符,没有重载“<”操作符,而是重载了“>”操作符就会报错。
反之,也可以显式地用greater<Key>作为比较器,此时就必要重载Key类中的“>”操作符了。
附:stl中map和set的声明,二者比较像,底层都是用红黑树实现的
template < class Key, class Compare = less<Key>,
class Allocator = allocator<Key> > class set;
template < class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > > class map;
template < class Key, class Compare = less<Key>,
class Allocator = allocator<Key> > class multiset;
template < class Key, class T, class Compare = less<Key>,
class Allocator = allocator<pair<const Key,T> > > class multimap;
从上面的声明可以看出,也可以定义一个函数对象Compare,声明map或set类型时传进入,如:
struct TTimeCompare
{
bool operator ()(const CTimerEvent* po1, const CTimerEvent* po2)const
{
return (po1->m_oNextTick < po2->m_oNextTick);
}
};
typedef multiset<CTimerEvent*, TTimeCompare> TEventSet;
struct ltstr // less than
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
set<const char*, ltstr> stSet; // set<Key, Compare, Alloc>
map<const char*, int, ltstr> stMap; // map<Key, Data, Compare, Alloc>
struct eqstr // equal
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) == 0;
}
};
hash_map<const char*, int, hash<const char*>, eqstr> stHashMap; // hash_map<Key, Data, HashFcn, EqualKey, Alloc>
// 自定义hash函数
namespace std
{
template<>
struct hash<KEY_TYPE>
{
size_t operator()(const KEY_TYPE& key) const
{
//return key.Hash();
}
};
}
相关文章推荐
- 结构体作为map的key或放入set中,需要重载<运算符
- 结构体作为map的key或放入set中,需要重载<运算符
- 结构体作为map的key或放入set中,需要重载<运算符
- 结构体作为STL map的key时需要注意什么? (某公司招聘面试试题)已跪~~~~(>_<)~~~~
- C++结构体作为map的key的时候需要重载<运算符
- groovy 使用变量值作为 map 的key--需要注意的
- 自定义结构作为map的key,比较函数写法
- 为实现非系统类作为Map的key需要覆写hashCode()和equals()方法
- 结构体作为STL map的key时需要注意什么? (某公司招聘面试试题)
- struct作为map的key时,需要重载该结构体
- Cassandra 的数据存储结构——本质是SortedMap<RowKey, SortedMap<ColumnKey, ColumnValue>>
- 自定义的类型放入STL的set中,需要重载自定义类中的“<”符号(转)
- 使用map来进行票数统计工作,循环输入多个人名,作为key存储到map中,对应的value就是该人获得的票数(即重复输入的次数),当重复输入时,需要对相应的数据进行修改。最红输quit结束循环,打印
- 自定义结构作为map的key,比较函数写法
- 如何根据CSV格式的第一列作为key,将后几列作为字符串放入map集合并写入文件的操作
- 对象作为 map 的 key 时,需要重写 equals 方法和 hashCode 方法
- 在hibernate中 以Collection<Entity> 替换Map<Key, List<Value>结构
- 从数据库取出的Date类型的数据放入Map<String,Object>之前需要做的处理
- 元素为自定义复合结构时 map,set 如何处理重复 key 及排序?
- 使用结构struct作为Dictionary<TKey,TValue>的键