【问题标题】:What is wrong with my contains function? [closed]我的 contains 函数有什么问题? [关闭]
【发布时间】:2013-05-22 21:37:53
【问题描述】:

我正在尝试创建一个映射来存储键值对。我写了一个ContainsKey 函数——如果我找到了密钥,则返回true,否则返回false

我认为我的 EQ 声明有问题,但我不知道问题出在哪里。有人可以看看我的代码并给我一些指导吗?

这是我的头文件“exer36.h”

#ifndef worksheet36_exer36_h
#define worksheet36_exer36_h

#ifndef DYARRAYDICTH
# define DYARRAYDICTH
/*
 dynamic array dictionary interface file
 */
# ifndef KEYTYPE
# define KEYTYPE char *
# endif
# ifndef VALUETYPE
# define VALUETYPE double
# endif


struct association {
    KEYTYPE key;
    VALUETYPE value;
};


# define TYPE struct association *
# include "exer14.h"
/* dictionary */
int dyArrayDictionaryContainsKey (struct DynArr * da, KEYTYPE key);

# endif

#endif

源文件“exer36.c”

#include <stdio.h>
#include <stdlib.h>
#include "exer36.h"

int dyArrayDictionaryContainsKey (struct DynArr *da, KEYTYPE key){

    for (int i=0; i < da->size; i++)
    {
     if (EQ(((struct association *)da->data[i])->key,key))
        return 1;
    }

    return 0;
}

头文件“exer14.h”

#ifndef worksheet14_exer14_h
#define worksheet14_exer14_h

#ifndef TYPE
#define TYPE int
#endif

# ifndef LT
# define LT(A, B) ((A) < (B))
# endif

#ifndef EQ
#define EQ(a, b) (a == b)
#endif

#endif


struct DynArr {
    TYPE *data;     /* pointer to the data array */
    int size;       /* Number of elements in the array */
    int capacity;   /* capacity ofthe array */
};


/* Dynamic Array Functions */
void initDynArr(struct DynArr *v, int capacity);
void freeDynArr(struct DynArr *v);
int sizeDynArr( struct DynArr *v);
void addDynArr(struct DynArr *v, TYPE val);
TYPE getDynArr(struct DynArr* da, int position);
void putDynArr(struct DynArr* da, int position, TYPE value);
void swapDynArr (struct DynArr* da, int i, int j);
void removeAtDyArr(struct DynArr* da, int index);

主文件

#include <stdio.h>
#include <stdlib.h>
#include "exer36.h"


int main(int argc, const char * argv[])
{

    //Create a dynamic array
    struct DynArr myArrD;
    printf("The size of myArrD should be empty (0), return: %d\n", myArrD.size);

    //Add a pair (Key = a AND Value = 1.0)
    char * a = "a";
    dyArrayDictionaryPut(&myArrD, a, 1.0);
    printf("Added one association. Size of myArrD should be 1, return: %d\n", myArrD.size);

    printf("%d\n",dyArrayDictionaryContainsKey(&myArrD, a));

    return 0;
}

【问题讨论】:

  • 使用 myArrD 的未初始化值不是一个好主意。它在堆栈上 == 默认情况下它包含垃圾。
  • 这个问题似乎是题外话,因为根据下面OP的“答案”,这个内容没有问题需要解决。

标签: c data-structures map contains containskey


【解决方案1】:

我认为问题在于您的KEYTYPEchar *,而您的EQ 宏只是一个普通的旧==。当您将两个char *== 进行比较时,您是在测试指针是否相等,而不是它们指向的值是否相等。您可能希望使用来自&lt;string.h&gt;strcmp 来比较键的值。

因为您的 contains 函数接受 char *,并且我假设它仅使用带引号的字符串(例如 dyArrayDictionaryContainsKey(myDynArr, "this is a key"))来调用,所以它可能总是返回 false,因为您正在比较 指针。

此外,我认为值得一提的是,尝试通过定义“模板化”您的地图......不太理想。我建议研究将任意数据类型存储为void *'s,并让调用者将它们转换回它们输入的类型,以及使用函数指针来处理诸如比较等之类的事情。

【讨论】:

  • 感谢您的提示。我试过“strcmp”,但它不起作用。
  • 您可以使用strcmp 发布您的代码吗?我可以向你保证:strcmp("my string", "my string") == 0,所以如果不是这种情况,那么我猜你要么不正确地比较字符串,要么检查错误的返回值。 如果字符串相等strcmp 返回 0(可能)令人困惑。
  • &CmdrMoozy:你好。这是我的代码: int dyArrayDictionaryContainsKey (struct DynArr *da, KEYTYPE key){ for (int i=0; i size; i++) { if (strcmp((((struct association *)(da->data [i]))->key),key) == 0)// 返回 1; } 返回 0;请让我知道我错过了什么。非常感谢。
【解决方案2】:

我的问题已解决。问题不在我在此处发布的代码中。它来自程序中的另一个现有函数。

【讨论】:

  • 既然这样,你为什么不删除问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 2016-07-07
  • 2013-02-11
  • 2014-06-26
  • 2015-02-04
  • 2017-12-11
相关资源
最近更新 更多