logo

Функция C++ map find().

C++ карта намирам() функцията се използва за намери елемент с даденото ключова стойност k . Ако намери елемента, тогава връща итератор, сочещ към елемента. В противен случай връща итератор, сочещ към края на картата, т.е. map::end().

Синтаксис

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

Параметър

к : Указва ключа за търсене в контейнера на картата.

Върната стойност

Ако намери елемента, тогава връща итератор, сочещ към елемента. В противен случай връща итератор, сочещ към края на картата, т.е. map::end().

ад за обратно извикване в javascript

Пример 1

Нека видим прост пример за намиране на елемента с дадена стойност на ключ.

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

В горния пример функцията find() се използва за намиране на елемент според зададената от потребителя стойност на ключ.

частично производно в латекс

Пример 4

Нека да видим един прост пример.

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