C++ vector 最大值和最大值的位置

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    vector<int> a = { 2,4,6,7,1,10,24,8,9,6,3,2 };
    vector<int>::iterator maxPosition = max_element(a.begin(), a.end());
    cout << *maxPosition << " at the postion of " << maxPosition - a.begin() <<endl;
   maxPosition) << endl;
    return 0;
}

  • 输出
    24 at the postion of 6

值得注意的是这里的迭代器"vector::iterator"也可以简写为auto,它能能够自动识别

猜你喜欢

转载自blog.csdn.net/Xurui_Luo/article/details/106750369