例如,我有这个数组:
int myArray[] = { 3, 6, 8, 33 };
如何检查给定变量x是否在其中?
我是否必须编写自己的函数并循环数组,还是现代c ++中有in_array
与PHP等效的函数?
您可以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::find
x
如果x
没有找到,则返回第一次出现的迭代器,或者返回范围的末尾的迭代器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句