例如:
if (value == array[size]) //if the value (unique) is present in an array then do something
这是否可以在一条语句中完成而无需调用函数或基本的 for 循环语句?
最佳答案
std::find
可以在一条语句中完成,但它不像其他语言那样微不足道:(
int array[10];
if (array + 10 != find(array, array + 10, 7)) {
cout << "Array contains 7!";
}
或用std::count
:
if (int n = count(array, array + 10, 7)) {
cout << "Array contains " << n << " 7s!";
}
关于c++ - 在一个语句中将一个值与所有数组元素进行比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3145727/