[C++ MAP]map多key值查找问题
请高人多多指教...
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
-------------------------------------------------------------*/
助人为乐.....先谢谢了...
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
-------------------------------------------------------------*/
助人为乐.....先谢谢了...
...全文
相关推荐
C++ STL 中这四个非常重要的关联容器:map, set, multimap, 和 multiset 1
C++ STL 中这四个非常重要的关联容器:map, set, multimap, 和 multiset 1
散列表之链接法解决冲突
散列表在进行映射的时候经常会发生冲突,这里采用链接法来解决链接法映射冲突带来的问题
基于C++ map中key使用指针问题的详解
C++实际开发的过程会经常使用到map。map是一个key-value值对,key唯一,可以用find进行快速的查找。其时间复杂度为O(logN),如果采用for循环进行遍历数据时间复杂度为O(N)。如果map中的数据量比较少时,采用find和for循环遍历的效率基本没有太大的区别,但是在实际的开发过程中,存储在map中的数据往往是大量的,这个时候map采用find方式效率比遍历效率高的多。 确定采用find方式查找数据后,我们需要考虑存储map的空间复杂度,对于基础数据类型的数据(int char等)这里就不做讨论。本文讨论的是map中存储的数据结构struct情况。1、如果map中的key
C++ map 根据value找key的实现
今天小编就为大家分享一篇C++ map 根据value找key的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
C++如何删除map容器中指定值的元素详解
map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。


