logo

خوارزمية C++ ماكس ()

خوارزمية C++ الأعلى() يمكن استخدام الوظيفة باتباع 3 طرق:

  • فهو يقارن بين القيمتين اللتين تم تمريرهما في وسيطاته و إرجاع الأكبر بينهما . إذا كان كلاهما متساويا، فإنه يعيد الأول.
  • كما أنه يقارن بين القيمتين باستخدام أ وظيفة ثنائية والتي تم تعريفها من قبل المستخدم، ثم تم تمريرها كوسيطة في 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 

معامل

أ : القيمة الأولى للمقارنة.

ب : القيمة الثانية للمقارنة.

شركات : دالة أصلية ثنائية محددة من قبل المستخدم تقبل وسيطتين وترجع صحيحًا إذا كانت الوسيطتان بالترتيب وإلا فإنها تُرجع خطأ. ويتبع الترتيب الضعيف الصارم لترتيب العناصر.

ال : قائمة التهيئة بالقيم المراد مقارنتها.

هاشت مقابل هاشماب

قيمة الإرجاع

تقوم بإرجاع الحد الأقصى لـ a و b. إذا كانت القيم متكافئة، فإنها ترجع a.

إرجاع أكبر قيمة في 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; } 

انتاج:

ماذا يعني xd
 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; } 

انتاج:

قراءة ملفات json
 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&apos;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);>