MAP--复杂map结构的构造
2013-10-21 15:11
183 查看
我的关键结构比如
struct{
int a;
int b;
int c;
}s;
因为这三个数据是基本信息,可以唯一区别一个设备。拿这样一个数据结构作为索引就能找到每个设备。
我现在想这么用
map<s, string>
因为map是二叉树,好像没法拿结构体比较大小,去索引,所以把结构体s改成类,重载小于号,让他能比较大小。
class s
{
public:
int a;
int b;
int c;
s(int m, int d, int u){a=m;b=d;c=u;}
bool operator < (const s &other)
{
if ((a<other.a) ||
((a==other.a)&&(b<other.b)) ||
((a==other.a)&&(b==other.b)&&(c<other.c)))
{
return true;
}
return false;
}
};
然后,
map<s, string> w;
s s1;
string s2;
一旦执行w.insert(make_pair(s1, s2));只要有这行就立刻报错。
要想使用一个类似结构体的数据结构作为KEY到底要怎么做呀?
是不是光重载一个小于号不够呀?
我现在好糊涂。有没有简单办法?
1.1
struct s {
int a;
int b;
int c;
bool operator<(const s&) const {
return true; }
};
map<s,string> m;
m.insert( make_pair(s(),"") );
1.2
struct s {
int a;
int b;
int c;
};
bool operator<(const s&,const s&) { return true; }
map<s,string> m;
m.insert( make_pair(s(),"") );
2.
struct s {
int a;
int b;
int c;
};
struct cmp {
bool operator()(const s&,const s&) const { return true; }
};
map<s,string,cmp> m;
m.insert(make_pair(s(),"" ) );
转载自:http://bbs.chinaunix.net/thread-1538318-1-1.html
struct{
int a;
int b;
int c;
}s;
因为这三个数据是基本信息,可以唯一区别一个设备。拿这样一个数据结构作为索引就能找到每个设备。
我现在想这么用
map<s, string>
因为map是二叉树,好像没法拿结构体比较大小,去索引,所以把结构体s改成类,重载小于号,让他能比较大小。
class s
{
public:
int a;
int b;
int c;
s(int m, int d, int u){a=m;b=d;c=u;}
bool operator < (const s &other)
{
if ((a<other.a) ||
((a==other.a)&&(b<other.b)) ||
((a==other.a)&&(b==other.b)&&(c<other.c)))
{
return true;
}
return false;
}
};
然后,
map<s, string> w;
s s1;
string s2;
一旦执行w.insert(make_pair(s1, s2));只要有这行就立刻报错。
要想使用一个类似结构体的数据结构作为KEY到底要怎么做呀?
是不是光重载一个小于号不够呀?
我现在好糊涂。有没有简单办法?
1.1
struct s {
int a;
int b;
int c;
bool operator<(const s&) const {
return true; }
};
map<s,string> m;
m.insert( make_pair(s(),"") );
1.2
struct s {
int a;
int b;
int c;
};
bool operator<(const s&,const s&) { return true; }
map<s,string> m;
m.insert( make_pair(s(),"") );
2.
struct s {
int a;
int b;
int c;
};
struct cmp {
bool operator()(const s&,const s&) const { return true; }
};
map<s,string,cmp> m;
m.insert(make_pair(s(),"" ) );
转载自:http://bbs.chinaunix.net/thread-1538318-1-1.html
相关文章推荐
- MAP--复杂map结构的构造
- jquery前端递归打印出树状结构的多层复杂map或json键值对数据
- json字符串转为map结构,复杂json字符串转为map结构
- JS构造Map结构
- Ibatis SQL Map组件结构分析
- 《计算机程序的解释和构造》(SICP)学习2-对系统结构的两种不同的“世界观”
- 数据结构与算法 —— 向量的扩容策略与分摊时间复杂度
- JavaScript构造XML树结构
- js之从Map结构对象中取值
- 利用Map解决复杂业务
- dense_hash_map的复杂key的用法
- 从零开始_学_数据结构(五)——STL(map、set、list、vector)
- c bdmap结构
- 【c++程序】复杂抽象结构
- Java——复杂的选择结构
- C++深度理解复杂链表的构造复制
- Jackson 处理复杂类型(List,map)
- java json转map(复杂混合类型list、map和string)
- unordered_map与线性结构内存占比
- 第4章:复杂选择结构