ในบทความนี้ เราจะพูดถึงการทำงาน ไวยากรณ์ และตัวอย่างของฟังก์ชัน valarray::max() ใน C++ STL
วาลาร์เรย์คืออะไร
std::valarray เป็นคลาสที่ใช้สำหรับแสดง ปรับเปลี่ยนอาร์เรย์ของค่า ซึ่งสนับสนุนการดำเนินการทางคณิตศาสตร์ที่ชาญฉลาดด้วยองค์ประกอบ
valarray::max() คืออะไร
std::valarray::max() ฟังก์ชั่นเป็นฟังก์ชัน inbuilt ใน C++ STL ซึ่งกำหนดไว้ในไฟล์ส่วนหัว
หาก valarray ว่างเปล่า ผลลัพธ์ที่ส่งคืนจะไม่ถูกระบุ
ไวยากรณ์
V_array_name.max();
พารามิเตอร์
ฟังก์ชันไม่ยอมรับพารามิเตอร์ -
คืนค่า
ฟังก์ชันนี้จะคืนค่าสูงสุดของวาลาร์เรย์
ตัวอย่าง
อินพุต
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