C++ 如何检查数组中是否存在给定的 int?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19299508/
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 22:41:27  来源:igfitidea点击:

How can I check if given int exists in array?

c++arrays

提问by rsk82

For example, I have this array:

例如,我有这个数组:

int myArray[] = { 3, 6, 8, 33 };

How to check if given variable x is in it?

如何检查给定的变量 x 是否在其中?

Do I have to write my own function and loop the array, or is there in modern c++ equivalent to in_arrayin PHP?

我是否必须编写自己的函数并循环数组,或者在现代 C++ 中是否有与in_arrayPHP等效的函数?

回答by juanchopanza

You can use std::findfor this:

您可以std::find为此使用:

#include <algorithm> // for std::find
#include <iterator> // for std::begin, std::end

int main () 
{
  int a[] = {3, 6, 8, 33};
  int x = 8;
  bool exists = std::find(std::begin(a), std::end(a), x) != std::end(a);
}

std::findreturns an iterator to the first occurrence of x, or an iterator to one-past the end of the range if xis not found.

std::findx如果x未找到,则返回指向第一次出现的的迭代器,或者返回超出范围末尾的迭代器。

回答by Zac Howland

I think you are looking for std::any_of, which will return a true/false answer to detect if an element is in a container (array, vector, deque, etc.)

我认为您正在寻找std::any_of,它将返回真/假答案以检测元素是否在容器中(数组、向量、双端队列等)

int val = SOME_VALUE; // this is the value you are searching for
bool exists = std::any_of(std::begin(myArray), std::end(myArray), [&](int i)
{
    return i == val;
});

If you want to know where the element is, std::findwill return an iterator to the first element matching whatever criteria you provide (or a predicate you give it).

如果您想知道元素在哪里,std::find将返回一个迭代器到匹配您提供的任何条件(或您提供的谓词)的第一个元素。

int val = SOME_VALUE;
int* pVal = std::find(std::begin(myArray), std::end(myArray), val);
if (pVal == std::end(myArray))
{
    // not found
}
else
{
    // found
}

回答by John Dibling

You almost never have to write your own loops in C++. Here, you can use std::find.

您几乎不需要用 C++ 编写自己的循环。在这里,您可以使用std::find

const int toFind = 42;
int* found = std::find (myArray, std::end (myArray), toFind);
if (found != std::end (myArray))
{
  std::cout << "Found.\n"
}
else
{
  std::cout << "Not found.\n";
}

std::endrequires C++11. Without it, you can find the number of elements in the array with:

std::end需要 C++11。没有它,您可以使用以下命令找到数组中的元素数:

const size_t numElements = sizeof (myArray) / sizeof (myArray[0]);

...and the end with:

...最后是:

int* end = myArray + numElements;

回答by Duncan Smith

Try this

尝试这个

#include <iostream>
#include <algorithm>


int main () {
  int myArray[] = { 3 ,6 ,8, 33 };
  int x = 8;

  if (std::any_of(std::begin(myArray), std::end(myArray), [=](int n){return n == x;}))   {
      std::cout << "found match/" << std::endl;
  }

  return 0;

}

}

回答by Neil Kirk

int index = std::distance(std::begin(myArray), std::find(begin(myArray), end(std::myArray), VALUE));

Returns an invalid index (length of the array) if not found.

如果未找到,则返回无效索引(数组的长度)。

回答by zirror

You do need to loop through it. C++ does not implement any simpler way to do this when you are dealing with primitive type arrays.

你确实需要遍历它。当您处理原始类型数组时,C++ 没有实现任何更简单的方法来执行此操作。

also see this answer: C++ check if element exists in array

另请参阅此答案:C++ 检查数组中是否存在元素