C++ 如何在C++中搜索字符串数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15606745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:32:54  来源:igfitidea点击:

How to search array of strings in C++

c++stringsearch

提问by dodgerblue

I want to be able to search an array of strings in C++. I have this data:

我希望能够在 C++ 中搜索字符串数组。我有这个数据:

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

If the user types Bec, the program finds the name Foo Becky, 924-234-2314. If the user types Palmer, then the program should show Geri Palmer, 555-8787and Ron Palmer, 555-2783

如果用户键入Bec,程序会找到名称Foo Becky, 924-234-2314。如果用户键入Palmer,则程序应显示Geri Palmer, 555-8787Ron Palmer, 555-2783

Here is what I have so far:

这是我到目前为止所拥有的:

#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;
}

How do I search an array of strings in C++?

如何在 C++ 中搜索字符串数组?

回答by A4L

You have to use the findmethod of std::string. This function return the start postion of string searched for in the string searched in. If no match was found it returns nposwitch is actually just -1.

您必须使用查找的方法std::string。此函数返回在搜索字符串中搜索字符串的起始位置。如果未找到匹配项,则返回npos女巫实际上只是-1.

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

回答by JiuDong

You should use find, as A4L already mentioned. I just want to add that your use of cin.ignore will not work well if bad value entered. you need

您应该使用 find,正如 A4L 已经提到的那样。我只想补充一点,如果输入了错误的值,您对 cin.ignore 的使用将无法正常工作。你需要

cin.clear() 

also. see this linkfor more details.

还。有关更多详细信息,请参阅此链接

回答by Eric Leschinski

How to search an array of strings example in C++:

如何在 C++ 中搜索字符串数组示例:

This is the brute force search method. Brute force means means we step through the entire string array and at each index of the array we search for our matching string.

这就是蛮力搜索方法。蛮力意味着我们遍历整个字符串数组,并在数组的每个索引处搜索匹配的字符串。

#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;
        }
    }
}

Above code prints:

上面的代码打印:

abcd

You should be verbalizing the above code from top to bottom in your head like this:

你应该像这样在头脑中从上到下描述上面的代码:

An array of string called data is initialized to contain 4 elements and given four values pidgeon, abcd, 1234and %^&#. An int variable called size is created which represents the number of elements in the string array. A string variable called matchString is created which contains the string 'bc'.

串称为数据的阵列被初始化为包含4种元素和给定的四个值pidgeonabcd1234%^&#。创建了一个名为 size 的 int 变量,它表示字符串数组中的元素数。创建了一个名为 matchString 的字符串变量,其中包含字符串 'bc'。

The for loop index starts at position zero and increments by 1 until it reaches one less than the size of the array. So the for loop goes through: 0, 1, 2, 3. The first value of x is 0. The if statement resolves data[0] to be pidgeon. The find method is applied against that string and two parameters are passed in. The string to be matched, and the position of the first character in the string to be considered in the search (0).

for 循环索引从位置 0 开始并递增 1,直到它达到比数组的大小小 1 为止。所以for循环遍历:0, 1, 2, 3。x的第一个值是0。if语句将data[0]解析为pidgeon。find 方法应用于该字符串并传入两个参数。要匹配的字符串,以及要在搜索中考虑的字符串中第一个字符的位置 (0)。

if 'bc' exists within 'pidgeon' then it will return the position of the first character of the first match, otherwise it will print std::string:npos which is the maximum value for size_t designating not found.

如果 'pidgeon' 中存在 'bc',那么它将返回第一个匹配项的第一个字符的位置,否则它将打印 std::string:npos,这是 size_t 指定未找到的最大值。

bc does not exist in pidgeon so it skips the interior of the for loop. The for loop continues to index position 1. bc is contained in abcd. So that string is printed. When all the items are searched, the for loop ends and the program completes.

bc 在 pidgeon 中不存在,因此它跳过 for 循环的内部。for 循环继续索引位置 1。bc 包含在 abcd 中。所以该字符串被打印出来。搜索完所有项目后,for 循环结束,程序完成。