You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
911 B

#include <iostream>
#include <vector>
#include <algorithm> // 包含 std::max_element 和 std::min_element
int main() {
std::vector<int> vec = {3, 1, 4, 2, 5};
// 查找最大值及其索引
auto maxIt = std::max_element(vec.begin(), vec.end());
int maxValue = *maxIt;
int maxIndex = std::distance(vec.begin(), maxIt);
// 查找最小值及其索引
auto minIt = std::min_element(vec.begin(), vec.end());
int minValue = *minIt;
int minIndex = std::distance(vec.begin(), minIt);
// 输出向量内容
std::cout << "向量: ";
for (int num : vec) {
std::cout << num << " ";
}
std::cout << "\n";
// 输出最大值及其索引
std::cout << "最大值: " << maxValue << ",索引: " << maxIndex << "\n";
// 输出最小值及其索引
std::cout << "最小值: " << minValue << ",索引: " << minIndex << "\n";
return 0;
}