C++ 数组中的最大值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34315002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:21:43  来源:igfitidea点击:

Max In a C++ Array

c++arrays

提问by JohnnyOnPc

I am trying to find the 'biggest' element in a user made array ,by using the max function from the algorithm library/header.

我试图通过使用算法库/标头中的 max 函数在用户创建的数组中找到“最大”元素。

I have done some research on the cplusplus reference site but there I only saw how to compare two elements using the max function. Instead I am trying to display the maximum number using a function 'max' ,without having to make a 'for' loop to find it.

我在 cplusplus 参考站点上做了一些研究,但在那里我只看到了如何使用 max 函数比较两个元素。相反,我试图使用函数 'max' 显示最大数字,而不必进行 'for' 循环来找到它。

For example:

例如:

Array: array[]={0,1,2,3,5000,5,6,7,8,9}Highest value: 5000

数组:array[]={0,1,2,3,5000,5,6,7,8,9}最大值:5000

I have made this code but it gives me a bunch of errors, which can be the issue?

我已经制作了这段代码,但它给了我一堆错误,这可能是问题所在?

#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;
}

回答by Ardavel

max_elementis the function you need. It returns an iterator to the max element in given range. You can use it like this:

max_element是你需要的功能。它返回一个迭代器到给定范围内的最大元素。你可以这样使用它:

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

Here you can find more information about this function: http://en.cppreference.com/w/cpp/algorithm/max_element

在这里您可以找到有关此功能的更多信息:http: //en.cppreference.com/w/cpp/algorithm/max_element

回答by Marko Popovic

Here is a modification of your program that does what you want:

这是对您的程序进行的修改,可以执行您想要的操作:

#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;
}

Note that you had a bug in your program, you did not initialize the last element in your array. This would cause your array to contain junk value in the last element. I've fixed that by increasing n to 11. Note that this is OK because the condition in the for loop is i < n, which means that i can be at most 10, which is what you want.

请注意,您的程序中有一个错误,您没有初始化数组中的最后一个元素。这会导致您的数组在最后一个元素中包含垃圾值。我已经通过将 n 增加到 11 来解决这个问题。请注意,这是可以的,因为 for 循环中的条件是i < n,这意味着 i 最多可以是 10,这就是您想要的。

回答by vishal

You can also use std::arrayby #include<array>

您也可以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;
}

More info on std::array

有关std::array 的更多信息