【问题标题】:Linear Search in a Char Array -- C++ (Visual Studio 2005)字符数组中的线性搜索——C++ (Visual Studio 2005)
【发布时间】:2010-10-28 15:01:17
【问题描述】:

我对 C++ 编程非常陌生,你会明白为什么。

我想创建一个由几个单词组成的字符数组,我想使用线性搜索函数进行搜索。这个数组必须是二维数组吗?例如:

char Colors[3][6] = {"red", "green", "blue"};

我试过这样:

char Colors[] = {"red", "green", "blue"};

这给了我一个“太多初始化程序”的错误。

我假设第一种方法是正确的,因为它说明了数组中元素的数量和元素的最大长度,对吗?

现在我将如何实现一个线性搜索函数来在该数组中查找一个单词?我可以执行以下操作吗:

(假设已经声明了linearSearch函数)

char searchKey;  
char element;

char Colors[3][6] = {"red", "green", "blue"};

printf("Enter the color to look for: \n");

scanf("%s", searchKey);

element = linearSearch(Colors, searchKey, ??); //?? is where I don't know what to enter

if (element != -1)  
{  
    printf("Found the word.\n");  
}  
else  
{  
    printf("Didn't find the word.\n");  
}

这可能吗?如果是这样,该声明会查找 linearSearch 函数吗?我希望我提供了足够的信息,以便它有点用处。

编辑:感谢大家的帮助,让程序按预期运行。

【问题讨论】:

  • 您的代码看起来不像此窗口中的代码。要解决此问题,请编辑您的问题(上面的链接),然后选择您的代码并单击带有小 1 和 0 的按钮。

标签: c++ arrays search visual-c++-2005


【解决方案1】:

This article 包含字符串搜索功能。它还应该让您深入了解如何正确构建字符数组。

【讨论】:

    【解决方案2】:

    我建议学习 C++ 标准库,这对你很有帮助。例如,

    #include <vector>
    #include <string>
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    vector<string> words;
    words.push_back("red");
    words.push_back("blue");
    words.push_back("green");
    
    if (find(words.begin(), words.end(), "green") != words.end())
        cout << "found green!"
    else
        cout << "didn't find it";
    

    为什么要自己实现linearSearch? c++ 已经有 std::find 为你做这件事! 此外,如果您使用 set 而不是 vector,您现在可以使用 std::binary_search,它是 O(log n) 而不是 O(n),因为集合已排序。

    【讨论】:

    • 这里是对 STL(C++ 标准模板库)的简短介绍:mochima.com/tutorials/STL.html
    • 是的,作为初学者并不意味着你必须为自己制造困难。使用标准库。这就是它的用途。 :)
    • 一旦我对 C++ 进行了更多的练习,我将研究 STL。感谢您的链接!
    【解决方案3】:

    您可以使您的 linearSearch 函数返回数组中搜索词的索引。这是一个示例程序:

    #include <stdio.h>
    #include <string.h>
    
    int linearSearch (const char **Array, const char *searchKey, int arraySize) {
        for (int i = 0; i < arraySize; ++i) {
            if (strcmp(Array[i], searchKey) == 0)
                return i;
        }
    
        // We didn't find the searchKey in the Array
        return -1;
    }
    
    int main () {
        char *colors[] = { "red", "green", "blue" };
    
        int index = linearSearch (colors, "green", 3);
    
        if (index < 0) { // search string was not found
            printf("Search string not found!\n");
        }
        else {
            printf("String %s found at index %d\n", colors[index], index);
        }
    
        return 0;
    }
    

    我们使用 strcmp() 函数来比较字符串。如果字符串匹配则返回零,如果不匹配则返回非零。要使用它,您需要包含 string.h 标头。

    但是,正如其他人所建议的,您应该尽可能使用 STL。

    【讨论】:

    • 我太无能了。我将如何声明这个函数?这就是我遇到的麻烦。在我声明这个之后,我将如何使用它?我尝试将其与 Tom Leys 的建议一起使用。我完全处于静止状态。
    • 太棒了,这行得通。是否可以查找“searchKey”而不是仅一种颜色,例如您的示例中的“绿色”,或者您可以搜索的内容是否存在限制?
    • 我不太确定你在问什么。您可以在数组中搜索您想要的任何字符串(例如“我是字符串”)。如果数组包含字符串,此函数将返回字符串在数组中的索引。如果数组不包含字符串,它将返回 -1。如果你有char searchKey[] = "Find me";在您的程序中,您可以将其用作函数的第二个参数。我希望这能回答你的问题。
    • 对不起,我的描述应该更清楚。我的意思是我是否可以让用户输入要在数组中查找的单词。假设我声明“char word;”然后 "printf("输入要查找的单词:\n");"然后 "scanf("%s", word);" ——类似的东西。然后将任何输入“word”的内容与“colors”数组进行比较。我希望这会有所帮助。
    • 是的,你可以这样做。但是您可能应该声明一个 char word[256] 或 char *word = (char *) malloc(256*sizeof(char))。 char word 将只是一个字符。当然,您可以使用任何您认为合适的尺寸来代替 256。
    【解决方案4】:

    如果您不想使用字符串,而使用 char 数组,您可以使用 strcmp 来比较 2 个单词。 strcmp 要记住的是它返回找到单词的位置的索引,所以如果你只想在开头找到单词,你可以这样做:

    for(int i=0;i<SizeOfColorArray;i++)
    {
        if(strcmp (MySearchTerm,colors[i]) == 0)
        {
            // it was  a match
            return i;
        }
    }
    

    根据您正在执行的操作以及您的数组将获得多大,您应该考虑考虑使用 hashes 的字符串来提高性能。

    【讨论】:

      【解决方案5】:

      要声明一个字符串数组,请使用以下语法

       char *Colors[] = {"red", "green", "blue"};
      

      这是一个指向字符的指针数组(“Hi”计算为指向“H”的 const char*)。编译器将计算出存储数组需要多少元素(因此是 []),在这种情况下,它的大小始终为 3。

      总的来说,我同意 rlbond 的回答 - 您应该使用 STL。

      【讨论】:

        【解决方案6】:

        不,你不需要二维数组。

        这是一种声明字符串数组的方法:

        char* Colors[3] = {"red", "green", "blue"};
        

        或

        char* Colors[] = {"red", "green", "blue"}; // the compiler will figure out the size
        
        int colors = sizeof(Colors)/sizeof(Colors[0]);
        

        在尝试过 C++ 之后,您应该立即学习使用 STL。

        【讨论】:

          猜你喜欢
          • 2010-10-03
          • 1970-01-01
          • 1970-01-01
          • 2020-02-01
          • 2010-10-12
          • 1970-01-01
          • 2018-05-23
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多