int arr[] = {0, 1, 15, 22, 100, 101, 232, 2323};
int n = sizeof(arr) / sizeof(arr[0]);
int x = 232;
auto a = lower_bound(arr, arr + n, x) - arr;
if (a < n && arr[a] == x) {
// x found at index a
cout << x << " is found " << "at index: " << a << endl;
}
Run Code Online (Sandbox Code Playgroud)
lower_bound 返回元素的地址,但如果我们从中减去 - arr,它将取消引用指针。怎么会这样?
| 归档时间: |
|
| 查看次数: |
50 次 |
| 最近记录: |