CodeSlapper
CodeSlapper

Reputation: 39

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

#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? I was hoping I could make this array behave like an iterator with a template.

Upvotes: 0

Views: 1165

Answers (1)

timrau
timrau

Reputation: 23058

It should be max_element(), not max().

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

Upvotes: 3

Related Questions