C++ mapa pronaći() funkcija se koristi za pronaći element s datim ključna vrijednost k . Ako pronađe element, vraća iterator koji pokazuje na element. Inače, vraća iterator koji pokazuje na kraj karte, tj. map::end().
Sintaksa
iterator find (const key_type& k); // until C++ 11 const_iterator find (const key_type& k) const; //since C++ 11
Parametar
k : Određuje ključ koji se traži u spremniku karte.
Povratna vrijednost
Ako pronađe element, vraća iterator koji pokazuje na element. U suprotnom, vraća iterator koji pokazuje na kraj mape, tj. map::end().
java break out loop
Primjer 1
Pogledajmo jednostavan primjer za pronalaženje elementa sa zadanom vrijednošću ključa.
#include #include using namespace std; int main(void) { map m = { {'a', 100}, {'b', 200}, {'c', 300}, {'d', 400}, {'e', 500}, }; auto it = m.find('c'); cout << 'Iterator points to ' <first << '=" <second << endl; return 0; } </pre> <p> <strong>Output:</strong> </p> <pre> Iterator points to c = 300 </pre> <p>In the above example, find() function returns the value of a given key value " c'.< p> <h2>Example 2</h2> <p>Let's see a simple example to find the element.</p> <pre> #include #include using namespace std; int main(void) { map m = { {'a', 100}, {'b', 200}, {'c', 300}, {'d', 400}, {'e', 500}, }; auto it = m.find('e'); if ( it == m.end() ) { // not found cout<<'element not found'; } else { found cout << 'iterator points to ' <first s see a simple example.< p> <pre> #include #include using namespace std; int main() { int n; map example = {{1,'a'},{2,'b'},{3,'c'},{4,'d'},{5,'e'} }; cout<>n; auto search = example.find(n); if (search != example.end()) { cout << n<<' found and the value is ' <first << '; } else { cout n<<' not found '; < pre> <p> <strong>Output:</strong> </p> <pre> Enter the element which you want to search: 4 4 found and the value is 4 = d </pre> <p>In the above example, find() function is used to find the element according to user?s given key value.</p> <h2>Example 4</h2> <p>Let's see a simple example.</p> <pre> #include #include using namespace std; int main () { map mymap; map::iterator it; mymap['a']=50; mymap['b']=100; mymap['c']=150; mymap['d']=200; it = mymap.find('b'); if (it != mymap.end()) mymap.erase (it); // print content: cout << 'elements in mymap:' << ' '; cout < ' <second << ' '; cout < ' <second return 0; } pre> <p> <strong>Output:</strong> </p> <pre> elements in mymap: a => 50 c => 150 d => 200 </pre></second></pre></'></pre></'element></pre></first>
U gornjem primjeru, funkcija find() koristi se za pronalaženje elementa prema korisnikovoj vrijednosti ključa.
greška atributa python
Primjer 4
Pogledajmo jednostavan primjer.
#include #include using namespace std; int main () { map mymap; map::iterator it; mymap['a']=50; mymap['b']=100; mymap['c']=150; mymap['d']=200; it = mymap.find('b'); if (it != mymap.end()) mymap.erase (it); // print content: cout << 'elements in mymap:' << ' '; cout < ' <second << \' \'; cout < \' <second return 0; } pre> <p> <strong>Output:</strong> </p> <pre> elements in mymap: a => 50 c => 150 d => 200 </pre></second>\'>\'element>