Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何检查数组中是否存在给定的int?_C++_Arrays - Fatal编程技术网

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

C++ 如何检查数组中是否存在给定的int?,c++,arrays,C++,Arrays,例如,我有一个数组: int myArray[] = { 3, 6, 8, 33 }; 如何检查给定的变量x是否在其中 我必须编写自己的函数并循环数组,或者在现代C++中,在PHP?> P>中,我的代码是等价于 iNoxs>代码>,我想你正在寻找,它将返回一个真/假答案来检测元素是否在容器(数组、向量、DeGy等)中。 如果您想知道元素的位置,将向第一个元素返回一个迭代器,该元素匹配您提供的任何条件(或您提供的谓词) 您可以为此使用: #include <algorithm> //

例如,我有一个数组:

int myArray[] = { 3, 6, 8, 33 };
如何检查给定的变量x是否在其中


我必须编写自己的函数并循环数组,或者在现代C++中,在PHP?

> P>中,我的代码是等价于<代码> iNoxs>代码>,我想你正在寻找,它将返回一个真/假答案来检测元素是否在容器(数组、向量、DeGy等)

中。 如果您想知道元素的位置,将向第一个元素返回一个迭代器,该元素匹配您提供的任何条件(或您提供的谓词)

您可以为此使用:

#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);
}
#include//for std::find
#包含//用于标准::开始,标准::结束
int main()
{
INTA[]={3,6,8,33};
int x=8;
bool exists=std::find(std::begin(a),std::end(a),x)!=std::end(a);
}

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

您确实需要对其进行循环。在处理原始类型数组时,C++没有实现任何更简单的方法。


也看到这个答案:

你几乎不需要在C++中编写自己的循环。在这里,你可以使用

…最后以:

int* end = myArray + numElements;
如果找不到,则返回无效索引(数组长度)。

尝试此操作

#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;
#包括
#包括
int main(){
int myArray[]={3,6,8,33};
int x=8;
if(std::任意_of(std::begin(myArray),std::end(myArray),[=](int n){return n==x;})){

std::cout看一看这个例子。是的,它确实是--std::find。它最终还是一个循环,但已经被编写好了。链接的问题甚至有一个答案涉及std::find_first_of…
std::find
std::find_first_of
std::any_of
。它们都实现了循环,但语言已经为它们提供了在C++中,大多数时候,你很少需要编写自己的循环。语言为原始数组提供了 STD::(c)(r)(开始)结尾的代码重载,以便使用统一的语法来处理它们。原始数组可以用作迭代器。当然,迭代器是指针的超集。或者简单地说:
bool exists=std::any_of(std::begin(array),std::end(array),[&](int i){return i==x;})如果C++不在C++ 11上,那么查找< < /C> >是好的,如果你不在C++ 11上。那么,开始< <代码> >代码> STD:结束< /代码>是C++ 11。我怎么能在一个非硬编码的数组上执行它?错误的“没有匹配函数”调用“STD::开始”,像这个代码> int *ScAsReServer标记:new int [[] ],int x=8;布尔存在= STD::查找(STD::开始)(squaresPlayerMarked),std::end(a),squaresPlayerMarked)!=std::end(a);std::cout@MoteZart您只有一个指针,因此您需要跟踪数组长度,然后使用它来获得结束迭代器的过去,例如
squaresPlayerMarked+5
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";
}
const size_t numElements = sizeof (myArray) / sizeof (myArray[0]);
int* end = myArray + numElements;
int index = std::distance(std::begin(myArray), std::find(begin(myArray), end(std::myArray), VALUE));
#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;