2013-03-25 41 views
0

我希望能夠在C++中搜索字符串數組。我有這樣的數據:如何在C++中搜索字符串數組

"Foo Becky, 924-334-2514", 
"Becky Warren, 555-1223", 
"Geri Palmer, 555-8787", 
"Ron Palmer, 555-2783" 

如果用戶鍵入Bec,程序找到名稱Foo Becky, 924-234-2314。如果用戶鍵入Palmer,則程序應該顯示Geri Palmer, 555-8787Ron Palmer, 555-2783

這是我到目前爲止有:

#include <iostream> 
#include <string> 
using namespace std; 
int main(){ 
    int n; 
    string search; 

    while(1){ 
     cout << "How many data you want to input: "<< endl; 
     cin >> n; 
     cin.ignore(1000, 10); 

     if(n > 0 && n < 20){ 
      break; 
     } 
     cout << "Number of data can not be negative or more than 20. "<< endl; 
    } 

    string* data = new string[n]; 

    for(int i=0; i< n; i++){ 
     cout << "Enter [First Name] [Last Name], [Phone-Number] and then hit " 
      << "enter." << endl << "e.g: Foo Becky, 925-245-413"<< endl; 
     getline(cin,data[i]); 
     cout << endl; 
    } 

    cout << "Enter what you want to search for: "<< endl; 
    getline(cin, search); 

    for(int i =0; i< n; i++){ 
     if(search == data[i]){ 
      cout << data[i]<< endl; 
     } 
    } 
    delete [] data; 
    return 0; 
} 

如何搜索在C++字符串數組?

+5

那麼你的問題到底是什麼?你有你的代碼和任務,但沒有問題。 – taocp 2013-03-25 02:35:07

+3

我會迴應@SongWang說的,但我會根據你的代碼推斷你的問題。你的基本邏輯或多或少都是正確的,但使用'=='將不起作用。您可能想了解['std :: string :: find'](http://www.cplusplus.com/reference/string/string/find/) – 2013-03-25 02:36:14

+0

我很抱歉,我沒有清楚地問。所以我的問題是,如何讓程序搜索用戶想要在之前填充的數組中搜索的內容。但是,儘管用戶只搜索「Palmer」,但該程序必須同時顯示「Geri Palmer,555-8787」和「Ron Palmer,555-2783」 – dodgerblue 2013-03-25 03:12:48

回答

1

您必須使用std::stringfind方法。該函數返回搜索到的字符串中搜索到的字符串的起始位置。如果未找到匹配項,則返回npos女巫實際上只是-1

if(data[i].find(search, 0) != std::string::npos); 
{ 
    cout << data[i]<< endl; 
} 
+0

嗨,謝謝你的回覆。代碼似乎沒有爲我工作,再加上我沒有得到它在什麼0爲(搜索,0) – dodgerblue 2013-03-25 03:35:10

+1

你真的應該閱讀我留下的評論。它會回答你的問題,如果你只關心鏈接到http://www.cplusplus.com/reference/string/string/find/,你會發現有關'std :: string :: search'函數的信息「0」是什麼意思。 – 2013-03-25 04:27:52

1

你應該使用find,因爲已經提到過A4L。我只想補充一點,如果輸入了錯誤的值,那麼使用cin.ignore將無法正常工作。你需要

cin.clear() 

也。有關更多詳細信息,請參閱this link

+0

謝謝你的補充,我的老師從來沒有說過,那真是太棒了 – dodgerblue 2013-03-25 03:44:45

0

如何搜索字符串例的陣列中的C++:

這是蠻力搜索方法。蠻力意味着我們遍歷整個字符串數組,並在數組的每個索引處搜索匹配的字符串。

#include<iostream> 
#include<string> 
using namespace std; 
int main(){ 
    //Create a structure to hold the data: 
    string data[] = {"pidgeon", "abcd", "1234", "%^*#"}; 

    //Get the length of the array. 
    int size = 4; 

    //loop through all the items and print them if they match 
    string matchString = "bc"; 
    for(int x = 0; x < size; x++){ 
     if (data[x].find(matchString, 0) != std::string::npos){ 
      cout << data[x] << endl; 
     } 
    } 
} 

以上代碼打印:

abcd 

你應該用言語表達從頂部上面的代碼底部在你的腦袋像這樣:

稱爲數據字符串數組初始化爲包含4元素並給出四個值pidgeon,abcd,1234%^&#。將創建一個名爲size的int變量,它表示字符串數組中的元素數。一個名爲matchString的字符串變量被創建,其中包含字符串'bc'。

for循環索引從零開始並增加1,直到達到小於數組大小的一個。所以for循環會經過:0,1,2,3。x的第一個值是0. if語句將數據[0]解析爲pidgeon。 find方法應用於該字符串,並傳入兩個參數。要匹配的字符串以及要搜索的字符串中第一個字符的位置(0)。

如果'pc'存在於'pidgeon'內,那麼它將返回第一個匹配的第一個字符的位置,否則它將打印std :: string:npos,這是指定未找到的size_t的最大值。

bc在pid中不存在,所以它跳過for循環的內部。 for循環繼續索引位置1. bc包含在abcd中。這樣就可以打印字符串了。當所有項目被搜索時,for循環結束並且程序完成。