#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;
return 0;
}
- 輸出
24 at the postion of 6
值得注意的是這裏的迭代器"vector::iterator"也可以改爲auto,它能夠自動識別對應的類型