简体   繁体   English

具有动态数组的std :: max

[英]std::max with a dynamic array

I'm trying to use the std libraries as much as possible and I have a question with std::max 我正在尝试尽可能使用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;
}

What is the best way to pass in the arguments to the std::max function here? 将参数传递给std :: max函数的最佳方法是什么? I was hoping I could make this array behave like an iterator with a template. 我希望我可以使该数组的行为像带有模板的迭代器。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM