logo

Fonction C++ map find()

Carte C++ trouver() la fonction est utilisée pour trouver un élément avec le donné valeur clé k . S'il trouve l'élément, il renvoie un itérateur pointant vers l'élément. Sinon, il renvoie un itérateur pointant vers la fin de la carte, c'est-à-dire map::end().

Syntaxe

 iterator find (const key_type& k); // until C++ 11 const_iterator find (const key_type& k) const; //since C++ 11 

Paramètre

k : Il spécifie la clé à rechercher dans le conteneur de carte.

Valeur de retour

S'il trouve l'élément, il renvoie un itérateur pointant vers l'élément. Sinon, il renvoie un itérateur pointant vers la fin de la carte, c'est-à-dire map::end().

css pour le gras

Exemple 1

Voyons un exemple simple pour trouver l'élément avec la valeur clé donnée.

 #include #include using namespace std; int main(void) { map m = { {&apos;a&apos;, 100}, {&apos;b&apos;, 200}, {&apos;c&apos;, 300}, {&apos;d&apos;, 400}, {&apos;e&apos;, 500}, }; auto it = m.find(&apos;c&apos;); cout &lt;&lt; &apos;Iterator points to &apos; <first << '=" &lt;second &lt;&lt; endl; return 0; } &lt;/pre&gt; &lt;p&gt; &lt;strong&gt;Output:&lt;/strong&gt; &lt;/p&gt; &lt;pre&gt; Iterator points to c = 300 &lt;/pre&gt; &lt;p&gt;In the above example, find() function returns the value of a given key value " c'.< p> <h2>Example 2</h2> <p>Let&apos;s see a simple example to find the element.</p> <pre> #include #include using namespace std; int main(void) { map m = { {&apos;a&apos;, 100}, {&apos;b&apos;, 200}, {&apos;c&apos;, 300}, {&apos;d&apos;, 400}, {&apos;e&apos;, 500}, }; auto it = m.find(&apos;e&apos;); if ( it == m.end() ) { // not found cout&lt;<'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,&apos;a&apos;},{2,&apos;b&apos;},{3,&apos;c&apos;},{4,&apos;d&apos;},{5,&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 ' <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&apos;s see a simple example.</p> <pre> #include #include using namespace std; int main () { map mymap; map::iterator it; mymap[&apos;a&apos;]=50; mymap[&apos;b&apos;]=100; mymap[&apos;c&apos;]=150; mymap[&apos;d&apos;]=200; it = mymap.find(&apos;b&apos;); if (it != mymap.end()) mymap.erase (it); // print content: cout &lt;&lt; &apos;elements in mymap:&apos; &lt;&lt; &apos;
&apos;; cout &lt; &apos; <second << '
'; cout < ' <second return 0; } pre> <p> <strong>Output:</strong> </p> <pre> elements in mymap: a =&gt; 50 c =&gt; 150 d =&gt; 200 </pre></second></pre></'></pre></'element></pre></first>

Dans l'exemple ci-dessus, la fonction find() est utilisée pour rechercher l'élément en fonction de la valeur clé donnée par l'utilisateur.

parcours d'arbre dans l'ordre

Exemple 4

Voyons un exemple simple.

 #include #include using namespace std; int main () { map mymap; map::iterator it; mymap[&apos;a&apos;]=50; mymap[&apos;b&apos;]=100; mymap[&apos;c&apos;]=150; mymap[&apos;d&apos;]=200; it = mymap.find(&apos;b&apos;); if (it != mymap.end()) mymap.erase (it); // print content: cout &lt;&lt; &apos;elements in mymap:&apos; &lt;&lt; &apos;
&apos;; cout &lt; &apos; <second << \'
\'; cout < \' <second return 0; } pre> <p> <strong>Output:</strong> </p> <pre> elements in mymap: a =&gt; 50 c =&gt; 150 d =&gt; 200 </pre></second>