基础知识,直接看代码:
#include <iostream>
#include <vector>
#include <algorithm>
int main(){
std::vector<int> a = { 2,4,6,7,1,0,8,9,6,3,2 };
auto maxPosition = std::max_element(a.begin(), a.end()); // 最小值min_element
std::cout << *maxPosition << " at the position of " << maxPosition - a.begin() << std::endl;
return 0;
}
输出:
9 at the position of 7
这段C++代码展示了如何使用标准库函数`std::max_element`找到一个整数向量的最大值及其在向量中的位置。程序输出最大值9及其在向量中的索引7。
6654

被折叠的 条评论
为什么被折叠?



