map自定义比较函数

template <class Key, class T, class Compare = less<Key>, class Alloc = alloc>

    第一个参数Key是关键字类型

第二个参数T是值类型

第三个参数Compare是比较函数(仿函数)

第四个参数是内存配置对象

 

Map中的关键字,起码必须有“<”这个比较操作符。我们知道,intfloatenumsize_t等等简单关键字,都有内置的比较函数,与map搭配无论是插入还是查找,都没什么问题。但是作为复杂数据类型,如果没有明确定义“<”比较操作符,就不能与map直接搭配使用,除非我们自己定义第三个参数。

在选择map的关键字时,注意以下两点:

a 关键字明确定义“<”比较操作符

b 没有“<”比较操作符,自定义仿函数替代第三个参数Compare,该仿函数实现“()”操作符,提供比较功能。插入时各节点顺序以该仿函数为纲。

 

一.             关键字明确定义“<”比较操作符

以友联函数代替函数内部定义的比较操作符,operator<为类的友元函数,代码如下:

#include "stdafx.h"

#include <iostream>

#include <string>

#include <map>

#include <iterator>

using namespace std;

 

struct myComp

{

    int abc;

    myComp():a(0), b(0), c(0){};

    myComp(int aaint bbint cc):a(aa), b(bb), c(cc){};

 

    friend bool operator< (const myComp &laconst myComp &lb);

};

 

inline bool operator< (const myComp &laconst myComp &lb)

{

    if (la.a != lb.areturn la.a<lb.a;

    if (la.b != lb.breturn la.b<lb.b;

    if (la.c != lb.creturn la.c<lb.c;

 

    return false;

}

 

int _tmain(int argc_TCHARargv[])

{

    map<myCompcharimap;

    imap[myComp(4,5,0)] = 'q';

    imap[myComp(2,8,9)] = 'o';

    imap[myComp(3,9,1)] = 'u';

    imap[myComp(3,10,1)] = '9';

    imap[myComp(3,9,0)] = '0';

 

    imap[myComp(11,11,11)] = 'g';

 

    map<myCompchar>::iterator iter = imap.begin();

    for (; iter != imap.end(); ++iter)

    {

       char temp[16] = {0};

       cout<<(iter->first).a<<" "<<(iter->first).b<<" "<<(iter->first).c<<" "<<iter->second<<endl;

    }

 

    getchar();

    return 0;

}

operator<函数中,返回的是“<”的比较结果,所以最终的结果也是从小到大排列。如果返回“>”的比较结果,那么最终是以从大到小排列。如果最后一步返回true,那么会出现abc值完全相同时,也可以同时插入map

 

以成员函数定义operator<operator<函数必须为const,代码如下:

#include "stdafx.h"

#include <iostream>

#include <string>

#include <map>

#include <iterator>

using namespace std;

 

struct myComp

{

    int abc;

    myComp():a(0), b(0), c(0){};

    myComp(int aaint bbint cc):a(aa), b(bb), c(cc){};

 

    bool operator< (const myComp &lbconst

    {

       if (a != lb.areturn a<lb.a;

       if (b != lb.breturn b<lb.b;

       if (c != lb.creturn c<lb.c;  

       return false;

    };

};

 

int _tmain(int argc_TCHARargv[])

{

    map<myCompcharimap;

    imap[myComp(4,5,0)] = 'q';

    imap[myComp(2,8,9)] = 'o';

    imap[myComp(3,9,1)] = 'u';

    imap[myComp(3,10,1)] = '9';

    imap[myComp(3,9,0)] = '0';

    imap[myComp(3,9,1)] = 'm';

    imap[myComp(11,11,11)] = 'g';

 

    map<myCompchar>::iterator iter = imap.begin();

    for (; iter != imap.end(); ++iter)

    {

       char temp[16] = {0};

       cout<<(iter->first).a<<" "<<(iter->first).b<<" "<<(iter->first).c<<" "<<iter->second<<endl;

    }

 

    getchar();

    return 0;

}

 

二.             自定义仿函数替代第三个参数

所谓的仿函数(functor),是通过重载()运算符模拟函数形为的类。
  因此,这里需要明确两点:
  1 仿函数不是函数,它是个类;
  2 仿函数重载了()运算符,使得它对你可以像函数那样子调用(代码的形式好像是在调用函数)

 

#include "stdafx.h"

#include <iostream>

#include <string>

#include <map>

#include <iterator>

using namespace std;

 

struct myComp

{

    int abc;

    myComp():a(0), b(0), c(0){};

    myComp(int aaint bbint cc):a(aa), b(bb), c(cc){};

};

 

struct CLOW

{

    bool operator() (const myComplconst myComprconst

    {

       if (l.a != r.areturn l.a<r.a;

       if (l.b != r.breturn l.b<r.b;

       if (l.c != r.creturn l.c<r.c;

       return false;

    }

};

 

int _tmain(int argc_TCHARargv[])

{

    map<myCompcharCLOWimap;

    imap[myComp(4,5,0)] = 'q';

    imap[myComp(2,8,9)] = 'o';

    imap[myComp(3,9,1)] = 'u';

    imap[myComp(3,10,1)] = '9';

    imap[myComp(3,9,0)] = '0';

    imap[myComp(3,9,1)] = 'm';

    imap[myComp(11,11,11)] = 'g';

 

    map<myCompcharCLOW>::iterator iter = imap.begin();

    for (; iter != imap.end(); ++iter)

    {

       char temp[16] = {0};

       cout<<(iter->first).a<<" "<<(iter->first).b<<" "<<(iter->first).c<<" "<<iter->second<<endl;

    }

 

    getchar();

    return 0;

}

 

其中CLOW是一个结构体,但是作为map的第三个参数传递过去之后,又因为重载了()操作符,使得CLOW这个结构体的用法像函数一样,这就是仿函数。

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值