免费注册 查看新帖 |

Chinaunix

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

[C++] map多key查找问题(续) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-10-14 08:40 |只看该作者 |倒序浏览
请高人多多指教...

1  一个map,两个key值,初始状态下两个key都有值。
2  key1与key2取值没有交集。
3  key1取值没有重复;key2取值没有重复。
4  查找时给一个值(a),事先知道(a)是key1类型的值还是key2类型的值。
    如果(a)是key1类型的值,则map中用(a)与key1做匹配进行查找;
    如果(a)是key2类型的值,则map中用(a)与key2做匹配进行查找;
5  会有给一个值(a)匹配到一个key1后,要同时得到key2的值的情况出现。

代码如下:
#include <iostream>
#include <string>
#include <map>

using namespace std;

//========================================================
typedef struct tagUKey
{
    char a[10]; // 如果给定一个值,在map中有一个a与之匹配,则还要返回b的值。
    char b[20];

    bool operator <(const struct tagUKey&ths) const
    {
        // 这里如何实现???
        return (((strcmp(a,ths.a)<0)||((strcmp(a,ths.a)>0)&& (strcmp(b,ths.b)<0)))&& (strcmp(b,ths.b)<0));
    }

} UKey;

map<UKey, int> gDat;
typedef map<UKey, int>::iterator      Dat_It;
typedef map<UKey, int>::value_type Dat_Val;

//========================================================
// 初始化map
void init()
{
    UKey s;
    strcpy(s.a,"11");
    strcpy(s.b,"4111");
    gDat.insert(Dat_Val(s,1));

    strcpy(s.a,"22");
    strcpy(s.b,"4222");
    gDat.insert(Dat_Val(s,2));

    strcpy(s.a,"33");
    strcpy(s.b,"4333");
    gDat.insert(Dat_Val(s,3));

    strcpy(s.a,"44");
    strcpy(s.b,"4444");
    gDat.insert(Dat_Val(s,4));

    strcpy(s.a,"55");
    strcpy(s.b,"4555");
    gDat.insert(Dat_Val(s,5));
}

//========================================================
// 根据key值查找
int findKey(int nFlag,char *pInfo,char *pOut)
{
    UKey s;
    switch(nFlag)
    {
    case 0: // 给定的值是要与key1匹配的
        strcpy(s.a,pInfo);
        break;
    case 1: // 给定的值是要与key2匹配的
        strcpy(s.b,pInfo);
        break;
    }

    Dat_It tmpIt;
    tmpIt = gDat.find(s);
    if (tmpIt != gDat.end())
    {
         printf("find. dat=%d\n",tmpIt->second);
         switch(nFlag)
        {
        case 0: // 找到匹配的key1,还要通过参数带出key2的值。
            strcpy(pOut,tmpIt->first.b);
            return tmpIt->second;
        case 1: // 找到匹配的key2,还要通过参数带出key1的值。
            strcpy(pOut,tmpIt->first.a);
            return tmpIt->second;
        }
    }
    else
        printf("not find.\n");

    return 0;
}

//========================================================
int main(int argc, char **argv)
{
    init();

    char szOut[20] = {0};
    int ret = 0;
    ret = findKey(0,"11",szOut);
    if (ret)
        printf("ret=%d,out=%s\n",ret,szOut);

    ret = findKey(1,"4111",szOut);
    if (ret)
        printf("ret=%d,out=%s\n",ret,szOut);

    ret = findKey(0,"22",szOut);
    if (ret)
        printf("ret=%d,out=%s\n",ret,szOut);

    ret = findKey(1,"4555",szOut);
    if (ret)
        printf("ret=%d,out=%s\n",ret,szOut);

    return 0;
}

/*-------------------------------------------------------------
输出为:

not find.          // 这里很奇怪,没找到。如何实现重载<,才好?
find. dat=1
ret=1,out=11
find. dat=2
ret=2,out=4222
find. dat=5
ret=5,out=55

-------------------------------------------------------------*/

助人为乐.....先谢谢了...

论坛徽章:
0
2 [报告]
发表于 2009-10-14 22:38 |只看该作者
I think map of map can do the trick.

map<key1, map<key2, int> >
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP