免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 9852 | 回复: 6
打印 上一主题 下一主题

想map的第一个元素使用结构体,是不是重载一下小于号就行了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-12 16:44 |只看该作者 |倒序浏览
我的关键结构比如
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到底要怎么做呀?
是不是光重载一个小于号不够呀?
我现在好糊涂。有没有简单办法?

[ 本帖最后由 urapple 于 2009-8-12 16:48 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2009-08-12 17:02 |只看该作者

回复 #1 urapple 的帖子

刚刚把这个class换成struct,通过了,不过为啥,不清楚。
typedef struct tags
{
public:
    int a;
    int b;
    int c;
    bool operator < (const tags &other) const
    {
        if ((a<other.a) ||
           ((a==other.a)&&(b<other.b)) ||
           ((a==other.a)&&(b==other.b)&&(c<other.c)))
        {
             return true;
         }
         return false;
    }
};

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
3 [报告]
发表于 2009-08-12 17:05 |只看该作者

回复 #1 urapple 的帖子

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(),"" ) );

论坛徽章:
0
4 [报告]
发表于 2009-08-12 17:24 |只看该作者

<必须重载为全局函数

#include <map>
#include <string>

using namespace std;

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 &one, const s &other)
{
        if ((one.a < other.a) || ((one.a == other.a) && (one.b < other.b)) ||
                ((one.a == other.a) && (one.b == other.b) && (one.c < other.c)))
                return true;
        return false;
}

int main(void)
{
        map<s, string> w;
        s s1(1, 2, 3);
        string s2;

        w.insert(make_pair(s1, s2));
        return 1;
}

论坛徽章:
0
5 [报告]
发表于 2009-08-12 17:52 |只看该作者

回复 #1 urapple 的帖子

谢谢两位。

论坛徽章:
0
6 [报告]
发表于 2009-08-12 17:57 |只看该作者

回复 #3 OwnWaterloo 的帖子

刚才没留意,map<s,string,cmp> m;
map可以有三个参数呀?

论坛徽章:
2
青铜圣斗士
日期:2015-11-26 06:15:59数据库技术版块每日发帖之星
日期:2016-07-24 06:20:00
7 [报告]
发表于 2009-08-12 18:09 |只看该作者

回复 #6 urapple 的帖子

其实还可以有第4个参数……
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP