2013-10-21 192 views
0

我試圖找到一個元素的索引數組中的字符串索引....我設法使其使用下面的函數整數工作:搜索陣列C++

int *getIndexOfInt(int *arr, int size, int check) { 
    int *result; 
    int *k; 
    int count = 0; 
    for (int i = 0; i <= size - 1; i++) { 
    if (arr[i] == check) { 
     k[count] = i; 
     count++; 
    } 

    } 
    if (count > 0) { 
    *result = *k; 
    return result; 
    } else 
    cout << "Not Found"; 
} 

然而,當我試圖這個字符串,它只是給了我錯誤或無限循環(具有狀態11退出程序):

int *getIndexOfString(string *arr, int size, string check) { 
    int *result; 
    int *k; 
    int count = 0; 
    for (int i = 0; i <= size - 1; i++) { 

     if (arr[i] == check) { 

      k[count] = i; 
      count++; 
      } 

    } 

    if (count > 0) { 

     *result = *k; 
     return result; 
    } 
    else cout << "Not Found"; 
} 

能否請你告訴我爲什麼,也許幫我修復錯誤?

編輯: 結果變量是其然後在主函數使用的陣列,它包含其中的字符串是在給定陣列中找到的索引。 k變量只是一個數組,其中的值在被添加到結果中之前被存儲。 arr是給定的字符串數組,大小是givven大小,check是代碼將搜索的字符串。

+0

乍一看錶明,您的第一個功能不起作用。 'int * k'和'int * result'永遠不會被初始化。 – yngccc

+0

然後我有另一個問題。第一個爲什麼工作? – SpiderLinked

+1

如果這不是訓練目的,我會建議使用['std :: find'](http://en.cppreference.com/w/cpp/algorithm/find)。 – Nobody

回答

3

首先,您正在訪問未初始化的內存。奇怪的是,你的第一個代碼工作。但是,它可能是編譯器特定的(這些事情很多都發生在C++中)。

局部變量通常分配在堆棧上,C++不保證您有任何默認值。因此,一種可能的解釋是存在(在相同的存儲器地址上,保存你的指針的地方)另一個有效的指針。現在,當你創建這個局部變量時,它只是得到這個「舊」地址,因此它正在訪問一些先前分配的內存。現在只是不關心它,即使它工作,相信我們 - 你不應該依賴這個。 :-)

另一個問題是與該返回值。當你不知道數組的大小時,你會如何使用它?你應該返回像std :: vector <>,某些結構或類似的東西。不只是指向未知長度的數組!

結果:你的代碼太複雜了。看到更好的解決方案:

#include <iostream> 
#include <string> 
#include <vector> 

std::vector<int> getIndexes(std::vector<std::string> &input, std::string searched) { 
    std::vector<int> result; 

    for (int i = 0; i < input.size(); i++) { 
     if (input[i] == searched) { 
      result.push_back(i); 
     } 
    } 

    return result; 
} 

int main(int argc, char *argv[]) { 
    std::vector<std::string> greetings; 
    greetings.push_back("hello"); 
    greetings.push_back("hi"); 
    greetings.push_back("bye"); 
    greetings.push_back("hi"); 
    greetings.push_back("hello"); 
    greetings.push_back("bye"); 

    std::vector<int> indexes = getIndexes(greetings, "hi"); 

    for (int i = 0; i < indexes.size(); i++) { 
     std::cout << indexes[i] << std::endl; 
    } 

    return 0; 
} 
+0

我測試了它..它工作正常...我沒有使用矢量庫..的唯一原因是因爲我從來沒有與它合作過...謝謝你的迴應。 – SpiderLinked

0

編輯的矢量使用:

std::vector<int> getIndexOfString(string *arr, int size, string check) 
{ 
    std::vector<int> iVect; 
    for (int i = 0; i <= size - 1; i++) 
    { 
    if (arr[i] == check) 
    { 
     iVect.push_back (i); 
     cout << "Found at " << i << endl; 
    } 
    } 

    if (iVect.empty) 
    cout << "not found" << endl; 

    return iVect; 
} 

你的函數有太多的未初始化的指針。你真的想要返回一個索引或指針嗎?您也錯過了失敗案例的返回值。

+0

我的代碼應該返回所有的索引...例如,如果givven數組是「a」,「b」,「a」它將輸出數組中的結果,在[0]我們將有1和在[1]我們將有3(如果我們搜索) – SpiderLinked

+0

@SpiderLinked我的錯誤,我錯過了你的要求的一部分。在這種情況下,我也強烈建議你返回一個向量(或者傳入一個向量)。編輯爲一個向量例子.... –

+0

謝謝你的迴應...這也是好的...(我沒有標記爲答案,因爲我不需要從函數的任何輸出,而這個函數應該在一個外部頭文件) – SpiderLinked

1

你的主要問題是你沒有初始化你的結果指針到一個有效的數組。真的,雖然你應該返回一個索引向量而不是指針,這樣調用者就知道這個大小,並且不需要手動管理內存。像這樣的:

#include <iostream> 
#include <vector> 
#include <string> 
#include <algorithm> 
#include <iterator> 

using namespace std; 

vector<int> getIndicesOfString(const vector<string>& in, const string& check) { 
    vector<int> ret; 
    for (auto it = cbegin(in); it != cend(in); ++it) { 
     if (*it == check) ret.push_back(distance(cbegin(in), it)); 
    } 
    return ret; 
} 

int main() { 
    auto v = vector<string>{"red", "orange", "yellow", "green", "blue", "indigo", "violet", "red"}; 
    auto indices = getIndicesOfString(v, "red"); 
    copy(cbegin(indices), cend(indices), ostream_iterator<int>(cout, ", ")); 
} 
0

其他人已經提出了它,並在這裏我把它作爲參考。

您可以使用標準庫。特別是算法std::find

#include<vector> 
#include<string> 
#include<iostream> 
#include<algorithm> 

int main() { 
    std::vector<std::string> words = {"one", "two", "three", "four", "five"}; 
    size_t index = std::distance(words.begin(), 
           std::find(words.begin(), words.end(), "three")); 
    std::cout<<"Index: "<<index<<std::endl; 
} 

編譯爲(GCC 4.8.1 OS X 10.7。4):

g++ indices-in-array.cpp -std=c++11 

輸出:

Index: 2 
+0

就像我說的..這是爲了訓練的目的...所以我不得不使用比find()更復雜的東西:) – SpiderLinked

+0

當然---就像我說過的:它在這裏供參考。其他人搜索Stackoverflow的答案,他們可能需要使用std :: find的實際代碼。 – Escualo