logo

مجموعة C++ تجد ()

مجموعة سي++ يجد() يتم استخدام الدالة ل العثور على عنصر مع المعطى قيمة فال. إذا عثر على العنصر، فإنه يُرجع مكررًا يشير إلى العنصر، وإلا فإنه يُرجع مكررًا يشير إلى نهاية المجموعة، أي set::end().

بناء الجملة

 iterator find (const value_type& val) const; // until C++ 11 const_iterator find (const value_type& val) const; //since C++ 11 iterator find (const value_type& val); //since C++ 11 

معامل

فال : يحدد القيمة المطلوب البحث عنها في الحاوية المحددة.

قيمة الإرجاع

إذا عثر على العنصر، فإنه يُرجع مكررًا يشير إلى العنصر، وإلا فإنه يُرجع مكررًا يشير إلى نهاية المجموعة، أي set::end().

تعقيد

لوغاريتمي في الحجم.

صلاحية التكرار

لا تغيرات.

سباقات البيانات

يتم الوصول إلى الحاوية (لا يقوم الإصدار const أو الإصدار غير const بتعديل الحاوية.

لم يتم الوصول إلى أي قيم معينة: يعد الوصول إلى العناصر وتعديلها بشكل متزامن أمرًا آمنًا.

سلامة الاستثناء

إذا تم طرح استثناء، فلن تكون هناك تغييرات في الحاوية.

مثال 1

دعونا نرى المثال البسيط للعثور على العنصر ذو القيمة الرئيسية المحددة:

 #include #include using namespace std; int main(void) { set m = {100,200,300,400}; auto it = m.find(300); cout << 'Iterator points to ' << *it << endl; return 0; } 

انتاج:

 Iterator points to 300 

مثال 2

دعونا نرى مثالا بسيطا للعثور على العنصر:

قائمة المصفوفات
 #include #include using namespace std; int main(void) { set m = {&apos;a&apos;, &apos;b&apos;, &apos;c&apos;, &apos;d&apos;}; auto it = m.find(&apos;e&apos;); if ( it == m.end() ) { // not found cout&lt;<'element not found'; } else { found cout << 'iterator points to ' *it<< endl; return 0; < pre> <p> <strong>Output:</strong> </p> <pre> Element not found </pre> <p>In the above example, find() function finds the key value e in the set m, if it is not found in the set then it will return a not found message otherwise, it will display the set.</p> <h2>Example 3</h2> <p>Let&apos;s see a simple example:</p> <pre> #include #include using namespace std; int main() { char n; set example = {&apos;a&apos;,&apos;b&apos;,&apos;c&apos;,&apos;d&apos;,&apos;e&apos;}; cout&lt;&gt;n; auto search = example.find(n); if (search != example.end()) { cout &lt;&lt; n&lt;<' found and the value is ' << *search '
'; } else { cout n<<' not found
'; < pre> <p> <strong>Output:</strong> </p> <pre> Enter the element which you want to search: b b found and the value is b </pre> <p>In the above example, find() function is used to find the element according to user&apos;s given value.</p> <h2>Example 4</h2> <p>Let&apos;s see a simple example:</p> <pre> #include #include int main () { std::set myset; std::set::iterator it; for (int i = 1; i <= 10; i++) myset.insert(i*10); it="myset.find(40);" myset.erase (it); (myset.find(60)); std::cout << 'myset contains:'; for (it="myset.begin();" it!="myset.end();" ++it) ' *it; '
'; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> myset contains: 10 20 30 50 70 80 90 100 </pre> <br></=></pre></'></pre></'element>

في المثال أعلاه، تعثر الدالة find() على قيمة المفتاح e في المجموعة m، وإذا لم يتم العثور عليها في المجموعة، فسوف تُرجع رسالة لم يتم العثور عليها، وإلا فستعرض المجموعة.

مثال 3

دعونا نرى مثالا بسيطا:

 #include #include using namespace std; int main() { char n; set example = {&apos;a&apos;,&apos;b&apos;,&apos;c&apos;,&apos;d&apos;,&apos;e&apos;}; cout&lt;&gt;n; auto search = example.find(n); if (search != example.end()) { cout &lt;&lt; n&lt;<\' found and the value is \' << *search \'
\'; } else { cout n<<\' not found
\'; < pre> <p> <strong>Output:</strong> </p> <pre> Enter the element which you want to search: b b found and the value is b </pre> <p>In the above example, find() function is used to find the element according to user&apos;s given value.</p> <h2>Example 4</h2> <p>Let&apos;s see a simple example:</p> <pre> #include #include int main () { std::set myset; std::set::iterator it; for (int i = 1; i <= 10; i++) myset.insert(i*10); it="myset.find(40);" myset.erase (it); (myset.find(60)); std::cout << \'myset contains:\'; for (it="myset.begin();" it!="myset.end();" ++it) \' *it; \'
\'; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> myset contains: 10 20 30 50 70 80 90 100 </pre> <br></=></pre></\'>

في المثال أعلاه، يتم استخدام وظيفة find() للعثور على العنصر وفقًا للقيمة المحددة للمستخدم.

مثال 4

دعونا نرى مثالا بسيطا:

 #include #include int main () { std::set myset; std::set::iterator it; for (int i = 1; i <= 10; i++) myset.insert(i*10); it="myset.find(40);" myset.erase (it); (myset.find(60)); std::cout << \'myset contains:\'; for (it="myset.begin();" it!="myset.end();" ++it) \' *it; \'
\'; return 0; } < pre> <p> <strong>Output:</strong> </p> <pre> myset contains: 10 20 30 50 70 80 90 100 </pre> <br></=>