logo

C++ set find()

C++ ingesteld vinden() functie wordt gebruikt een element vinden met het gegeven waarde val. Als het het element vindt, retourneert het een iterator die naar het element verwijst, anders retourneert het een iterator die naar het einde van de set wijst, d.w.z. set::end().

Syntaxis

 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 

Parameter

val : specificeert de waarde waarnaar moet worden gezocht in de ingestelde container.

Winstwaarde

Als het het element vindt, retourneert het een iterator die naar het element verwijst, anders retourneert het een iterator die naar het einde van de set wijst, d.w.z. set::end().

Complexiteit

Logaritmisch van formaat.

Iteratorgeldigheid

Geen veranderingen.

Dataraces

Er wordt toegang verkregen tot de container (noch de const- noch de niet-const-versies wijzigen de container.

Er wordt geen toegang verkregen tot toegewezen waarden: het gelijktijdig openen en wijzigen van de elementen is veilig.

Uitzondering Veiligheid

Als er een uitzondering optreedt, zijn er geen wijzigingen in de container.

voorbeeld 1

Laten we het eenvoudige voorbeeld bekijken om het element met de gegeven sleutelwaarde te vinden:

 #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; } 

Uitgang:

 Iterator points to 300 

Voorbeeld 2

Laten we een eenvoudig voorbeeld bekijken om het element te vinden:

alfabet nummer
 #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>

In het bovenstaande voorbeeld vindt de functie find() de sleutelwaarde e in de set m. Als deze niet in de set wordt gevonden, retourneert hij een niet gevonden bericht, anders wordt de set weergegeven.

Voorbeeld 3

Laten we een eenvoudig voorbeeld bekijken:

 #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></\'>

In het bovenstaande voorbeeld wordt de functie find() gebruikt om het element te vinden op basis van de door de gebruiker opgegeven waarde.

Voorbeeld 4

Laten we een eenvoudig voorbeeld bekijken:

 #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></=>