2009-05-24 51 views
1

我對C++編程非常陌生,你會明白爲什麼。在char數組中的線性搜索 - C++(Visual Studio 2005)

我想製作一個由我想用線性搜索功能搜索的幾個單詞組成的字符數組。這個數組是否必須是一個二維數組?例如:

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函數?我希望我提供了足夠的信息,以便稍微有用。

編輯:感謝所有的幫助,讓程序按預期工作。

+0

您的代碼看起來不像此窗口中的代碼。爲了解決這個問題,編輯你的問題(上面的鏈接),然後選擇你的代碼,並點擊其上有小1和0的按鈕。 – 2009-05-24 21:24:58

回答

2

您可以使您的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。

0

This article包含字符串搜索功能。它還應該讓您瞭解如何正確地構建您的字符數組。

9

我會推薦學習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),因爲集合已排序。

+0

這裏是一個不錯的小前奏到STL(C++標準模板庫):http://www.mochima.com/tutorials/STL.html – rlbond 2009-05-24 21:05:57

+1

是啊,作爲一個初學者,並不意味着你要爲難自己。使用標準庫。這就是它的目的。 :) – jalf 2009-05-24 21:07:48

0

如果你不想使用字符串,並留在char數組中,你可以使用strcmp來比較2個單詞。 與strcmp要記住的一點是,它返回這裏所說的被發現的指標,因此如果您想才發現的話你不喜歡這樣的遊戲內:

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

取決於你是什麼在做什麼和你的陣列有多大,你應該考慮尋找具有hashes的字符串來提高性能。

3

要聲明一個字符串數組,使用此語法

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

這是一個指針數組,以字符(「Hi」的計算結果爲一個const char *在「H」指向)。編譯器會計算出需要多少元素來存儲數組(因此[]),在這種情況下,它的大小始終爲3.

總體而言,我同意rlbond的回答 - 您應該使用STL。

2

不,你並不需要一個二維數組。

下面是聲明字符串數組的方式:

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試驗後。