📄 find.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta name="generator" content= "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org"> <title>find</title> <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table> <tr> <td> <div class="body-content"> <div class="header-box"> <a href="../index.html">cppreference.com</a> > <a href= "index.html">C++ Algorithms</a> > <a href="find.html">find</a> </div> <div class="name-format"> find </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <algorithm> iterator find( iterator start, iterator end, const <a href="../containers.html">TYPE</a>& val );</pre> <p>The find() algorithm looks for an element matching <em>val</em> between <em>start</em> and <em>end</em>. If an element matching <em>val</em> is found, the return value is an iterator that points to that element. Otherwise, the return value is an iterator that points to <em>end</em>.</p> <p>For example, the following code uses find() to search a vector of integers for the number 3:</p> <pre class="example-code"> int num_to_find = 3; vector<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back(i); } vector<int>::iterator result; result = find( v1.begin(), v1.end(), num_to_find ); if( result == v1.end() ) { cout << "Did not find any element matching " << num_to_find << endl; } else { cout << "Found a matching element: " << *result << endl; } </pre> <p>In the next example, shown below, the find() function is used on an array of integers. This example shows how the C++ Algorithms can be used to manipulate arrays and pointers in the same manner that they manipulate containers and iterators:</p> <pre class="example-code"> int nums[] = { 3, 1, 4, 1, 5, 9 }; int num_to_find = 5; int start = 0; int end = 2; int* result = find( nums + start, nums + end, num_to_find ); if( result == nums + end ) { cout << "Did not find any number matching " << num_to_find << endl; } else { cout << "Found a matching number: " << *result << endl; } </pre> <div class="related-name-format"> Related topics: </div> <div class="related-content"> <a href="adjacent_find.html">adjacent_find</a><br> <a href="find_end.html">find_end</a><br> <a href="find_first_of.html">find_first_of</a><br> <a href="find_if.html">find_if</a><br> <a href="mismatch.html">mismatch</a><br> <a href="search.html">search</a> </div> </div> </td> </tr> </table></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -