2013-03-25 119 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循环结束并且程序完成。