C++ אלגוריתם max()
אלגוריתם C++ מקסימום() ניתן להשתמש בפונקציה ב-3 דרכים הבאות:
- הוא משווה בין שני הערכים שהועברו בטיעונים שלו ו מחזיר את הגדול יותר ביניהם . אם שניהם שווים, הוא מחזיר את הראשון.
- זה גם משווה את שני הערכים באמצעות a פונקציה בינארית אשר מוגדר על ידי המשתמש, ולאחר מכן מועבר כארגומנט ב-std::max().
- הוא משמש גם כדי למצוא את הרכיב הגדול ביותר ברשימה נתונה , והוא מחזיר את הראשון אם יש יותר מאחד הגדולים ברשימה.
אלמנטים מושווים באמצעות אופרטור
תחביר
default (1) template const T& max (const T& a, const T& b); //until C++ 11 custom (2) template const T& max (const T& a, const T& b, Compare comp); //until C++ 11 default (1) template const T& max (const T& a, const T& b); //until C++ 14 custom (2) template const T& max (const T& a, const T& b, Compare comp); //until C++ 14 initializer list (3) template T max (initializer_list il); template T max (initializer_list il, Compare comp); //until C++ 14 default (1) template constexpr const T& max (const T& a, const T& b); //since C++ 14 //since C++ 14 custom (2) template constexp const T& max(const T& a, const T& b, Compare comp); // since C++ 14 initializer list (3) template constexpr T max (initializer_list il); template constexpr T max (initializer_list il, Compare comp); //since C++ 14
פָּרָמֶטֶר
א : ערך ראשון להשוואה.
ב : ערך שני להשוואה.
comp : פונקציית פרדיקט בינארית המוגדרת על ידי משתמש שמקבלת שני ארגומנטים ומחזירה true אם שני הארגומנטים מסודרים אחרת היא מחזירה false. זה עוקב אחר הסדר החלש הקפדני להזמין את האלמנטים.
ה : רשימת initializer_list עם הערכים להשוואה.
ערך החזרה
הוא מחזיר את המקסימום של a ו-b. אם הערכים שווים, הוא מחזיר א.
מחזירה את הערך הגדול ביותר ב-il. אם מספר ערכים שווים למקסימום, מחזיר את הערך השמאלי ביותר.
מוּרכָּבוּת
המורכבות היא ליניארית באחד פחות ממספר האלמנטים בהשוואה.
חריגים
פונקציה זו זורקת חריג אם כל השוואה זורקת חריג.
הערה: הפרמטרים הלא חוקיים גורמים להתנהגות לא מוגדרת.
דוגמה 1
בוא נראה את הדוגמה הפשוטה להדגמת השימוש ב-max():
#include #include #include using namespace std; int main() { cout << 'larger of 1 and 9999: ' << std::max(1, 9999) << '
' << 'larger of 'a', and 'b': ' << max('a', 'b') << '
' << 'longest of 'foo', 'bar', and 'hello': ' << max( { 'foo', 'bar', 'hello' }, [](const string& s1, const string& s2) { return s1.size() < s2.size(); }) << '
'; return 0; } תְפוּקָה:
larger of 1 and 9999: 9999 larger of 'a', and 'b': b longest of 'foo', 'bar', and 'hello': hello
דוגמה 2
בוא נראה דוגמה פשוטה נוספת להדגמת השימוש ב-max() באמצעות גרסת ברירת המחדל:
#include // std::cout #include // std::max using namespace std; int main () { cout << 'max(1,2)==' << max(1,2) << '
'; cout << 'max(2,1)==' << max(2,1) << '
'; cout << 'max('a','z')==' << max('a','z') << '
'; cout << 'max(3.14,2.73)==' << max(3.14,2.73) << '
'; return 0; } תְפוּקָה:
max(1,2)==2 max(2,1)==2 max('a','z')==z max(3.14,2.73)==3.14
דוגמה 3
בוא נראה דוגמה פשוטה נוספת להדגמת השימוש ב-max() באמצעות פונקציית השוואה:
#include #include using namespace std; // Defining the binary function bool comp(int a, int b) { return (a <b); } int main() { a="7;" b="28;" cout << max(a,b,comp) '
'; returns the first one if both numbers are same max(7,7,comp); return 0; < pre> <p> <strong>Output:</strong> </p> <pre> 28 7 </pre> <h2>Example 4</h2> <p>Let's see a simple example to find the maximum element in the list:</p> <pre> #include #include using namespace std; // Defining the binary function bool comp(int a, int b) { return (a <b); } int main() { finding the largest of all numbers cout << 'maximum element is: '<< max({1, 2, 3, 4, 5, 10, -1, 7},comp) '
'; return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Maximum element is: 10 </pre> <br></b);></pre></b);> דוגמה 4
בוא נראה דוגמה פשוטה כדי למצוא את הרכיב המקסימלי ברשימה:
#include #include using namespace std; // Defining the binary function bool comp(int a, int b) { return (a <b); } int main() { finding the largest of all numbers cout << \'maximum element is: \'<< max({1, 2, 3, 4, 5, 10, -1, 7},comp) \'
\'; return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Maximum element is: 10 </pre> <br></b);>