简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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