我在 C++ 的 algorithm 头文件中使用 min_element()
函数时遇到了问题。
代码如下:
int a[5] = {4, 1, 2, 3, 4};
for (int j = n - 1; j >= 0; j--) {
for (int i = 0; i <= j; i++) {
int *lowest = min_element(a+i, a+j); //get min element in range
cout << "A[" << i << "] to A[" << j << "]"
<< "lowest =" << *lowest << endl;
}
}
它的输出如下:
A[0] to A[4]lowest =1
A[1] to A[4]lowest =1
A[2] to A[4]lowest =2
A[3] to A[4]lowest =3
A[4] to A[4]lowest =4
A[0] to A[3]lowest =1
A[0] to A[2]lowest =1
"A[0] to A[1]lowest =4"
A[0] to A[0]lowest =4
当 i=0 和 j=1 时,它输出的是 '4' 而不是 '1'。 能否有人解释一下这是为什么?