C++ 如何检查字符串是否在字符串数组中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20303821/
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
How to check if string is in array of strings
提问by user3050705
#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 nstring is in tabarray, but the code return an error. What I am doing wrong? Maybe I should use the vectors?
如果n字符串在选项卡数组中,我想检查 C++ ,但代码返回错误。我做错了什么?也许我应该使用向量?
回答by masoud
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:
它不会告诉您 的大小array
,它会告诉您该数组中第一个字符串的长度,您应该将数组的长度单独传递给函数。该函数应如下所示:
bool in_array(string value, string *array, int length)
But a better choice is using std::vector
and std::find
:
但更好的选择是使用std::vector
and 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)) {
...
}
}
回答by leemes
When passing an array as an argument to a function which takes only a pointer, you can't query the sizeof 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.
当将数组作为参数传递给只接受一个指针的函数时,您无法在函数内查询数组的大小(因为它已转换为指向第一个元素的“愚蠢”指针,仅此而已)。您通常会向您的签名或“结束”迭代器添加一个“计数”参数。
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.
如果找到元素,将返回一个迭代器,否则返回结束迭代器。使用结束迭代器检查相等性将告诉您是否在数组中找到该元素。
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 算法的“迭代器接口”,则可以std::find
通过使用模板函数进行环绕来实现类似 PHP 的签名:
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
到编译器标志时才有效。