【问题标题】:doing binary search in an array of strings using bsearch c使用 bsearch c 在字符串数组中进行二进制搜索
【发布时间】:2020-05-19 17:06:28
【问题描述】:

所以我试图在一个名为 conj_str 的字符串数组中进行二进制搜索,要做的事情是我必须对其进行排序,而我试图使用 qsort 的问题是比较函数不起作用并且它没有排序任何东西.

程序:

#include<stdlib.h> 
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024
#define MAX_SIZE 10000

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char * const *arg = b;
  return strcmp(key, *arg);
}

int main()
{
 int i;
 char conj_str[MAX_SIZE][MAX_CHARS];
 size_t len = sizeof(conj_str)/sizeof(const char *);
 strcpy(conj_str[0],"fcb");
 strcpy(conj_str[1],"bvb");
 strcpy(conj_str[2],"slb");
 strcpy(conj_str[3],"fcp");
 strcpy(conj_str[4],"rma");
 qsort (conj_str, len, sizeof (const char *), compare);
 for (i = 0; i < 5; i++) {
 printf ("%d: %s\n", i, conj_str[i]);
 }
}

【问题讨论】:

  • qsort() 的大小参数和比较函数错误,conj_str 不包含字符指针
  • 那我该怎么办?
  • @pmg 这跟我的问题有什么关系?
  • 您从bsearch() 获得void* 值。您应该将其转换为char*,但您将其转换为int* 并将其分配给int???
  • 关于我想要该数组中字符串的位置

标签: c multidimensional-array c-strings qsort bsearch


【解决方案1】:

在本次通话中

qsort (conj_str, len, sizeof (const char *), compare);

错误地指定了数组元素的大小。应该有

qsort (conj_str, len, sizeof ( char[MAX_CHARS]), compare);

还有这个说法

int j = (int*) bsearch("fcb",conj_str,len,sizeof(const char *),compare);

没有意义。

并在比较函数中声明

  const char * const *arg = b;

也没有意义。

函数看起来像

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char *arg = b;
  return strcmp(key, arg);
}

并替换此语句

size_t len = sizeof(conj_str)/sizeof(const char *);

size_t len = 5;

这是您的程序,稍作改动。

#include<stdlib.h> 
#include<stdio.h>
#include <string.h>
#define MAX_CHARS 1024
#define MAX_SIZE 10000

int compare (const void *a, const void *b)
{
  const char *key = a;
  const char *arg = b;
  return strcmp(key, arg);
}

int main()
{
    int i;
    char conj_str[MAX_SIZE][MAX_CHARS];
    size_t len = 5;

 strcpy(conj_str[0],"fcb");
 strcpy(conj_str[1],"bvb");
 strcpy(conj_str[2],"slb");
 strcpy(conj_str[3],"fcp");
 strcpy(conj_str[4],"rma");
 qsort (conj_str, len, sizeof ( char[MAX_CHARS]), compare);
 for (i = 0; i < 5; i++) {
 printf ("%d: %s\n", i, conj_str[i]);
 }


 char ( *p )[MAX_CHARS] = bsearch("fcb",conj_str,len,sizeof(char[MAX_CHARS]),compare);

if ( p )
{
    printf( "Found at position %zu\n", ( size_t )(p - conj_str ) );
}
else
{
    puts( "Not found" );
}

}

它的输出是

0: bvb
1: fcb
2: fcp
3: rma
4: slb
Found at position 1

【讨论】:

  • 所以我这样做并删除了整个 bsearch 的东西,我得到了分段错误
【解决方案2】:

这里是你的代码的工作重写:

#include <stdlib.h> 
#include <stdio.h>
#include <string.h>

#define MAX_CHARS       1024
#define MAX_SIZE        5

int compare(const void *a, const void *b)
{
        return strcmp(a, b);
}

int main(void)
{
        int i;

        char conj_str[MAX_SIZE][MAX_CHARS];
        size_t len = sizeof(conj_str) / sizeof(conj_str[0]);
        strcpy(conj_str[0], "fcb");
        strcpy(conj_str[1], "bvb");
        strcpy(conj_str[2], "slb");
        strcpy(conj_str[3], "fcp");
        strcpy(conj_str[4], "rma");

        qsort(conj_str, len, sizeof(conj_str[0]), compare);

        for (i = 0; i < len; i++) {
                printf ("%d: %s\n", i, conj_str[i]);
        }

        char *s = bsearch("fcb", conj_str, len, sizeof(conj_str[0]), compare);
        puts(s ? "found" : "not found");
}

第一件事:数组中的每个字符串都应该被初始化(至少为空字符串),以便qsort()bsearch() 工作,这就是我放的原因:

#define MAX_SIZE        5

(也因为 10000 * 1024 对于堆栈上的数组来说太多了)

那么qsort()bsearch() 的大小参数都是错误的,在这种情况下应该是:

sizeof(conj_str[0])

因为它是数组中包含的元素的大小。

bsearch() 返回一个指向该元素的指针(如果已创建),则为 char *。 最后一行应该是:

puts(s ? "found" : "not found");

其中sbsearch() 的返回指针。

【讨论】:

  • 是的,因为 10000*1024 太大,我使用了这个 ulimit -s unlimited,这让我可以使用 10000
  • 好的,但是如果你只使用 5 个字符串,为什么需要 10000 个字符串的空间呢?
  • 好吧,我以 5 为例,但我想存储大量数据,我只放了 10k,以便有足够的空间来存储字符串
  • 我建议您研究动态内存分配和函数malloc()(和free())。不建议在堆栈上使用大数组。
  • 问题是我已经尝试过使用动态内存分配,但出现缓冲区溢出错误,我什至问我如何解决这个问题,人们试图帮助我,但没有任何效果,这就是为什么我使用像 10k 这样的大数字
猜你喜欢
  • 2021-04-15
  • 2013-05-22
  • 2023-03-24
  • 2020-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多