logo

C++ 지도 찾기() 함수

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())를 반환합니다.

자바의 목록 노드

실시예 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>