我正在寻找一种在数组中找到给定 int 的方法,我找到了这个解决方案
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int data[] = {23, 45, 56, 12, 34, 56};
int target = 56;
int arraySize = sizeof(data) / sizeof(*data);
bool isPresent = std::find(data, data + arraySize, target) != data + arraySize;
if (isPresent) {
cout << "The element is present";
} else {
cout << "The element is not present";
}
return 0;
Run Code Online (Sandbox Code Playgroud)
现在我测试了并且它有效,但我想知道为什么在 find() 之后有这个 != data + arraySize ?希望得到解释