Finding the position of the max element

后端 未结 5 1849
感情败类
感情败类 2020-12-04 11:15

Is there a standard function that returns the position(not value) of the max element of an array of values?

For example:

Suppose I have an array like this:

5条回答
  •  一生所求
    2020-12-04 11:28

    You can use max_element() function to find the position of the max element.

    int main()
    {
        int num, arr[10];
        int x, y, a, b;
    
        cin >> num;
    
        for (int i = 0; i < num; i++)
        {
            cin >> arr[i];
        }
    
        cout << "Max element Index: " << max_element(arr, arr + num) - arr;
    
        return 0;
    }
    

提交回复
热议问题