为什么输出要变成50,它本来应该是20。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int v[] = {10, 20, 30, 50, 20, 70, 30};
int* i1;
i1 = std::min_element(v + 3, v + 4);
cout << *i1 << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
lub*_*bgr 14
STL算法在半开范围内运行,通常表示为[first, last)。这意味着该first元素包含在范围内,该last元素不包含在内。因此
[v + 3, v + 4)
Run Code Online (Sandbox Code Playgroud)
指定一个长度为1的范围,并且该范围内唯一的元素具有值50。的结果*std::min_element(v + 3, v + 4)只能是50。