如何检查字符串是否在字符串数组中

时间:2021-01-22 14:09:35
#include <iostream>
#include <string>
using namespace std;

bool in_array(string value, string *array)
{
    int size = (*array).size();
    for (int i = 0; i < size; i++)
    {
        if (value == array[i])
        {
            return true;
        }
    }

    return false;
}

int main() {
    string tab[2] = {"sdasd", "sdsdasd"};
    string n;
    cin >> n;
    if (in_array(n, tab)) {

    }
    return 0;
}

I want to check in C++ if n string is in tab array, but the code return an error. What I am doing wrong? Maybe I should use the vectors?

如果有n个字符串在选项卡数组中,我想在c++中进行检查,但是代码返回了一个错误。我做错了什么?也许我应该用向量?

2 个解决方案

#1


23  

int size = (*array).size();

It will not tell you the size of array, it tells you the length of first string in that array, you should pass the length of array to the function separately. The function should look like:

它不会告诉你数组的大小,它会告诉你数组中第一个字符串的长度,你应该将数组的长度分别传递给函数。函数应该如下所示:

bool in_array(string value, string *array, int length)

 

 

But a better choice is using std::vector and std::find:

但是更好的选择是使用std::vector和std:::find:

bool in_array(const std::string &value, const std::vector<string> &array)
{
    return std::find(array.begin(), array.end(), value) != array.end();
}

and then, you can use it like:

然后,你可以这样使用它:

int main() {
    std::vector<std::string> tab {"sdasd", "sdsdasd"};

    if (in_array(n, tab)) {
        ...
    }
}

#2


5  

When passing an array as an argument to a function which takes only a pointer, you can't query the size of the array within the function (since it got converted to a "stupid" pointer to the first element, nothing more). You typically add a "count" parameter to your signature or an "end" iterator instead.

当将数组作为参数传递给只接受指针的函数时,您不能查询函数中数组的大小(因为它被转换为指向第一个元素的“愚蠢”指针,仅此而已)。通常,您将“count”参数添加到签名或“end”迭代器中。

What you're trying to implement is basically std::find. It takes two iterators (begin and end of the sequence) and the element to be found. Simply use this function.

你要实现的基本上就是std::find。它需要两个迭代器(序列的开始和结束)和要查找的元素。简单地使用这个函数。

std::find(std::begin(tab), std::end(tab), n);

will return an iterator to the element if it was found, the end iterator otherwise. Checking for equality with the end iterator will tell you if the element was found in the array.

如果找到元素,将返回一个迭代器,否则将返回结束迭代器。检查与end iterator是否相等将告诉您是否在数组中找到了元素。

If you don't like the "iterator interface" of the std algorithms, you can achieve your PHP-like signature by wrapping around std::find by using a template function:

如果您不喜欢std算法的“iterator接口”,可以通过使用模板函数:

template<class Element, class Container>
bool in_array(const Element & element, const Container & container)
{
    return std::find(std::begin(container), std::end(container), element)
            != std::end(container);
}

Please note: This answer assumes C++11. If you use an older compiler, it might not work or it only works if you add -std=c++11 to the compiler flags.

请注意:此答案假定为c++ 11。如果使用较旧的编译器,它可能不工作,或者只在将-std=c++11添加到编译器标志时才有效。

#1


23  

int size = (*array).size();

It will not tell you the size of array, it tells you the length of first string in that array, you should pass the length of array to the function separately. The function should look like:

它不会告诉你数组的大小,它会告诉你数组中第一个字符串的长度,你应该将数组的长度分别传递给函数。函数应该如下所示:

bool in_array(string value, string *array, int length)

 

 

But a better choice is using std::vector and std::find:

但是更好的选择是使用std::vector和std:::find:

bool in_array(const std::string &value, const std::vector<string> &array)
{
    return std::find(array.begin(), array.end(), value) != array.end();
}

and then, you can use it like:

然后,你可以这样使用它:

int main() {
    std::vector<std::string> tab {"sdasd", "sdsdasd"};

    if (in_array(n, tab)) {
        ...
    }
}

#2


5  

When passing an array as an argument to a function which takes only a pointer, you can't query the size of the array within the function (since it got converted to a "stupid" pointer to the first element, nothing more). You typically add a "count" parameter to your signature or an "end" iterator instead.

当将数组作为参数传递给只接受指针的函数时,您不能查询函数中数组的大小(因为它被转换为指向第一个元素的“愚蠢”指针,仅此而已)。通常,您将“count”参数添加到签名或“end”迭代器中。

What you're trying to implement is basically std::find. It takes two iterators (begin and end of the sequence) and the element to be found. Simply use this function.

你要实现的基本上就是std::find。它需要两个迭代器(序列的开始和结束)和要查找的元素。简单地使用这个函数。

std::find(std::begin(tab), std::end(tab), n);

will return an iterator to the element if it was found, the end iterator otherwise. Checking for equality with the end iterator will tell you if the element was found in the array.

如果找到元素,将返回一个迭代器,否则将返回结束迭代器。检查与end iterator是否相等将告诉您是否在数组中找到了元素。

If you don't like the "iterator interface" of the std algorithms, you can achieve your PHP-like signature by wrapping around std::find by using a template function:

如果您不喜欢std算法的“iterator接口”,可以通过使用模板函数:

template<class Element, class Container>
bool in_array(const Element & element, const Container & container)
{
    return std::find(std::begin(container), std::end(container), element)
            != std::end(container);
}

Please note: This answer assumes C++11. If you use an older compiler, it might not work or it only works if you add -std=c++11 to the compiler flags.

请注意:此答案假定为c++ 11。如果使用较旧的编译器,它可能不工作,或者只在将-std=c++11添加到编译器标志时才有效。