C++ 中 valarray 的 max() 函式


在本文中我們將討論 C++ STL 中 valarray::max() 函式的工作原理、語法和示例。

什麼是 valarray?

std::valarray 是一個用於表示、修改值陣列的類,它支援逐元素數學運算。

什麼是 valarray::max()?

std::valarray::max() 函式是 C++ STL 中的一個內建函式,在 <<valarray> 標頭檔案中定義。此函式返回 valarray 容器中的最大值。

如果 valarray 為空,則返回的結果未指定。

語法

V_array_name.max();

引數

該函式不接受引數 −

返回值

此函式返回 valarray 的最大值。

示例

輸入

valarray<int> arr = { 1, 2, 4, 5, 8, 9 };
arr.max();

輸出

9

示例

 動態演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   valarray<int> arr = {2, 4, 6, 8, 10};
   cout<<"Largest element is = "; cout<<arr.max() << endl;
   return 0;
}

輸出

Largest element is = 10

示例

 動態演示

#include <bits/stdc++.h>
using namespace std;
int main(){
   valarray<int> arr = {2, 4, 6, 10, 10};
   //finding out the square root of greatest number
   int product = arr.max() * arr.max();
   cout<<"Square root of greatest number is: "<<product;
   return 0;
}

輸出

Square root of greatest number is: 100

更新於:17-Apr-2020

240 次瀏覽

開啟您的職業生涯

完成課程,獲取認證

開始
廣告
© . All rights reserved.