C++ 中向量的最大值和最小值
- 
          
            在 C++ 中使用 for循环从向量中查找最大值和最小值
- 
          
            在 C++ 中使用 std::max_element和std::min_element函数从向量中获取最大值和最小值
- 
          
            C++ 中使用 std::minmax_element函数从向量中获取最大值和最小值
 
C++ 中的向量只不过是一个可以自动调整自身大小的动态数组。由于向量是元素的容器,我们可能想找出向量包含的最大值或最小值。
在处理向量时,我们可以像在数组中搜索最大或最小元素一样使用循环。本文还将介绍一些帮助我们做同样事情的库函数。
在 C++ 中使用 for 循环从向量中查找最大值和最小值
在下面的示例中,我们有两个函数模板; maxElement 用于查找最大元素,minElement 用于查找最小元素。在 main() 块中,我们定义了一个向量 marks 并将其传递给模板函数。
minElement 函数和 maxElement 函数的返回值分别存储在变量 min, 和 max 中,并将这些变量打印到屏幕上。maxElement 函数有一个变量 max,其中包含宏 INT_MIN 值,然后我们使用 for 循环遍历所有向量元素并将它们与存储在 max 中的值进行比较。
如果我们发现一个元素大于存储在其中的值,我们会更新 max 的值,并且对向量的所有元素都执行此操作。函数 maxElement 返回变量 max 的最后更新值,这就是我们从向量 marks 中获取最大值的方式。
minElement 函数也以相同的方式工作并返回向量的最小值。让我们举个例子,使用循环从 C++ 中的向量中找到最大值和最小值。
代码:
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
template <typename D>
int maxElement(vector<D> const &v) {
  int max = INT_MIN;
  for (const D &i : v) {
    if (max < i) {
      max = i;
    }
  }
  return max;
}
template <typename D>
int minElement(vector<D> const &v) {
  int min = INT_MAX;
  for (const D &i : v) {
    if (min > i) {
      min = i;
    }
  }
  return min;
}
int main() {
  vector<int> marks = {23, 45, 65, 23, 43, 67, 87, 12};
  int min = minElement(marks);
  int max = maxElement(marks);
  cout << "The minimum marks are: " << min << endl;
  cout << "The maximum marks are: " << max << endl;
  return 0;
}
输出:
The minimum marks are: 12
The maximum marks are: 87
这是一种在 C++ 中从向量中查找最大和最小元素的简单方法。
在 C++ 中使用 std::max_element 和 std::min_element 函数从向量中获取最大值和最小值
要在 C++ 中从向量中找到最大值和最小值,我们可以分别使用 std::max_element 和 std::min_element 函数。
max_element 函数返回一个指向最大值的迭代器,min_element 函数返回一个指向最小值的迭代器,两者都在 (start, end) 范围内。
语法:
*min_element(start_index, end_index);
*max_element(start_index, end_index);
在下面的示例中,我们首先将指向向量开始和结束的指针 marks 作为参数传递给函数 max_element 和 min_element。max_element 和 min_element 函数返回的值分别存储在变量 max 和 min 中。
代码:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> marks = {23, 34, 56, 75, 23, 44, 58};
  int max = *max_element(marks.begin(), marks.end());
  int min = *min_element(marks.begin(), marks.end());
  cout << "The minimum marks are: " << min << endl;
  cout << "The maximum marks are: " << max << endl;
  return 0;
}
输出:
The minimum marks are: 23
The maximum marks are: 75
请注意,这两个函数都可以在预定义函数的帮助下进行比较。在此处阅读有关这些功能的更多信息。
C++ 中使用 std::minmax_element 函数从向量中获取最大值和最小值
std::minmax_element 更像是上述两个函数的浓缩版本。我们可以使用 std::minmax_element 函数来获取一对迭代器作为返回值,而不是单独使用它们。
此函数将返回一对迭代器,其中第一个值指向最小元素,第二个值指向最大元素。例如,我们将向量的第一个和最后一个索引 marks 传递给函数 minmax_element,该函数返回的值存储在变量 res 中,该变量使用 auto 定义关键词。
然后,我们使用 dot(.) 运算符从这对迭代器中分离出最小值和最大值,并将它们存储在变量 min 和 max 中。我们使用 minmax_element 函数从 C++ 中的向量中获取最大值和最小值。
代码:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> marks = {34, 23, 56, 75, 23, 67, 88, 12};
  auto res = minmax_element(marks.begin(), marks.end());
  int min = *res.first;
  int max = *res.second;
  cout << "The minimum marks are: " << min << endl;
  cout << "The maximum marks are: " << max << endl;
  return 0;
}
输出:
The minimum marks are: 12
The maximum marks are: 88
下面是上述代码的修改版本,它还返回向量的最大值和最小值的索引。这是使用 std::distance 函数完成的,该函数计算第一个和最后一个元素之间的元素数。
代码:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  vector<int> marks = {34, 23, 56, 75, 23, 67, 88, 12};
  auto res = minmax_element(marks.begin(), marks.end());
  int min = *res.first;
  int max = *res.second;
  int indx_max = distance(marks.begin(), res.first);
  int indx_min = distance(marks.begin(), res.second);
  cout << "The minimum marks are: " << min << endl;
  cout << "Found at index: " << indx_min << endl;
  cout << "The maximum marks are: " << max << endl;
  cout << "Found at index: " << indx_max << endl;
  return 0;
}
输出:
The minimum marks are: 12
Found at index: 6
The maximum marks are: 88
Found at index: 7
要了解有关此功能的更多信息,请查看此文档。这就是我们如何从 C++ 中的向量中获取最大值和最小值的全部内容。