logo

C++ postavi find()

C++ set pronaći() funkcija se koristi za pronaći element s datim vrijednost val. Ako pronađe element, vraća iterator koji pokazuje na element, u suprotnom, vraća iterator koji pokazuje na kraj skupa, tj. set::end().

Sintaksa

 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 

Parametar

val : specificira vrijednost koja se traži u spremniku skupa.

Povratna vrijednost

Ako pronađe element, vraća iterator koji pokazuje na element, u suprotnom, vraća iterator koji pokazuje na kraj skupa, tj. set::end().

Složenost

Logaritamske veličine.

Valjanost iteratora

Nema promjena.

Utrke podataka

Spremniku se pristupa (ni const ni non-const verzije ne mijenjaju spremnik.

Ne pristupa se mapiranim vrijednostima: istovremeno pristupanje i mijenjanje elemenata je sigurno.

Iznimka Sigurnost

Ako se izbaci iznimka, nema promjena u spremniku.

Primjer 1

Pogledajmo jednostavan primjer za pronalaženje elementa sa zadanom vrijednošću ključa:

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

Izlaz:

 Iterator points to 300 

Primjer 2

Pogledajmo jednostavan primjer za pronalaženje elementa:

1 od 1000
 #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>

U gornjem primjeru funkcija find() pronalazi ključnu vrijednost e u skupu m, ako nije pronađena u skupu, vratit će poruku nije pronađena, u suprotnom će prikazati skup.

Primjer 3

Pogledajmo jednostavan primjer:

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

U gornjem primjeru, funkcija find() koristi se za pronalaženje elementa prema korisnikovoj vrijednosti.

Primjer 4

Pogledajmo jednostavan primjer:

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