C ++
 Computer >> コンピューター >  >> プログラミング >> C ++

C ++でSTLを使用して配列の最大要素を見つける方法は?


ここでは、最大要素を見つける方法を説明します。したがって、配列が[12、45、74、32、66、96、21、32、27]の場合、max要素は96です。algorithm.hヘッダーファイルにあるmax_element()関数を使用して、最大要素。

#include<iostream>
#include<algorithm>
using namespace std;
int main() {
   int arr[] = {12, 45, 74, 32, 66, 96, 21, 32, 27};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "Array is like: ";
   for (int i = 0; i < n; i++)
      cout << arr[i] << " ";
   cout << "\nMax Element is: " << *max_element(arr, arr + n);
}

出力

Array is like: 12 45 74 32 66 96 21 32 27
Max Element is: 96

  1. 二分探索を使用して配列内の最大要素を検索するC++プログラム

    これは、二分探索木を使用して配列の最大要素を見つけるためのC++プログラムです。このプログラムの時間計算量はO(log(n))です。 アルゴリズム Begin Construct the Binary Search Tree using the given data elements. Next traverse the root pointer to the rightmost child node available. Print the data part of the node as the maximum data element of the given data

  2. 配列内の最大要素を見つけるためのPHPプログラム

    配列内の最大要素を見つけるためのPHPコードは次のとおりです- 例 <?php    function get_max_value($my_array){       $n = count($my_array);       $max_val = $my_array[0];       for ($i = 1; $i < $n; $i++)          if ($max_val < $my_array[$i])