c++ - 带有动态数组的 std::max

标签 c++ dynamic standard-library

我正在尝试尽可能多地使用 std 库,但我对 std::max 有疑问

#include <algorithm>

void foo(size_t elem)
{
  auto testArray = new double[elem];
  for(int i = 0; i < elem; ++i)
  {
    testArray[i] = i;
  }

  auto maxElem = std::max(testArray, testArray + (elem - 1));
  delete[] testArray;
}

在这里将参数传递给 std::max 函数的最佳方式是什么?我希望我能让这个数组表现得像一个带有模板的迭代器。

最佳答案

应该是max_element(),不是max()

auto maxElem = *(std::max_element(testArray, testArray + elem));

关于c++ - 带有动态数组的 std::max,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40955303/

相关文章:

javascript - 动态标记无法使用新变量

java - MyBatis 中如何选择动态 fetchSize

c++ - 哪些常用的 C++ 环境缺乏对标准库的支持(即使不是全部,也是大部分)?

c++ - 为什么 strtol 和 strtok 的这种组合不起作用?

C++ 调用尚未定义的函数

c++ - 优化代码: fibonacci algorithm

c++ - 类方法声明中的错误 : Invalid use of void expression ,

c# - 如何处理具有相同父级的不同事件处理程序?

python - sys.stdin.read() 在 python 中到底返回什么?

c++ - 编写一个最小的自定义运算符:std::sort 需要为我的类型定义 std::__lg