首页 > 解决方案 > 在 *min_element (first_index, last_index); 函数,为什么 last_index 被排除在最小计算中?

问题描述

我预计下面的程序会给出 0 作为输出,但实际上它是 1。为什么考虑第一个元素而最后一个元素不考虑最小计算?

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> arr = {5,4,3,2,1,0};
    cout<<*min_element(arr.begin()+1,arr.begin()+5);
    return 0;
}

标签: c++vectorstl

解决方案


您的最终迭代器必须是最后一次才能按预期工作。

IE

arr.begin()+6

或者为什么不只是

arr.end()

推荐阅读