Max. In a C ++ array

I am trying to find the "largest" element in a user array using the max function from the library / header of the algorithm, I did some research on the cplusplus reference site, but there I only saw how to compare two elements using the max function. Instead, I try to display the maximum number using the max function without creating a "for" loop to find it. For example, we have an array [] = {0,1,2,3,5000,5,6,7,8,9}, so the largest number will be 5000. I made this code, but it gives me a bunch of errors, which might be a problem?

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int array[11];
int n = 10;
for (int i = 0; i < n; i++) {
    array[i] = i;
}
array[5] = 5000;
max(array , array + n);
for (int i = 0; i < n; i++)
    cout << array[i] << " ";
return 0;
}
+5
source share
3 answers

max_element - , . max . :

cout << " max element is: " << *max_element(array , array + n) << endl;

: http://en.cppreference.com/w/cpp/algorithm/max_element

+7

, , :

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int array[11];
    int n = 11;
    for (int i = 0; i < n; i++) {
        array[i] = i;
    }
    array[5] = 5000;

    cout << *std::max_element(array, array + n) << "\n";

    return 0;
}

, , . , . , n 11. , , for i < n, , 10, .

0

std::array #include<array>

#include <iostream>
#include <algorithm>
#include <array>
using namespace std;
int main()
{
array<int,10> arr;
int n = 10;
for (int i = 0; i < n; i++) {
arr[i] = i;
}
arr[5] = 5000;


cout<<"Max: "<< *max_element(arr.begin(),arr.end())<<endl;


for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}

std:: array

0

Source: https://habr.com/ru/post/1620362/


All Articles