如何根据以下代码段找到数组中的最大元素值

时间:2017-10-05 00:07:52

标签: c++

...
[  PASSED  ] 56 tests.

=================================================================
==12326==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 54 byte(s) in 3 object(s) allocated from:
    #0 0x7f4c634fd020 in strdup (/usr/lib64/libasan.so.3+0x58020)
    #1 0x301d215bb4  (/usr/lib64/libtbb.so.2+0x301d215bb4)

SUMMARY: AddressSanitizer: 54 byte(s) leaked in 3 allocation(s).
make[3]: *** [CMakeFiles/check] Error 1
make[2]: *** [CMakeFiles/check.dir/all] Error 2
make[1]: *** [CMakeFiles/check.dir/rule] Error 2
make: *** [check] Error 2

如何在数组中找到max

2 个答案:

答案 0 :(得分:1)

您可以使用std::max_element

max = *std::max_element(n, n+arrayLength);

答案 1 :(得分:1)

你可以编写暴力

int array_max = -1; // Or some very negative number.
for (int i = 0; i < arrayLength; ++i)
{
  if (n[i] > array_max)
  {
    array_max = n[i];
  }
}