4
我在C++的算法頭文件中遇到min_element()
問題。無法正確使用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 」。
有人可以解釋一下嗎?
謝謝你,先生,現在問題得到解決 – RDK
@ user3291897:很高興它幫助!您可以[接受](http://stackoverflow.com/help/accepted-answer)我的回答(謝謝!:)) –