c++中std::find_std :: find()与C ++中的示例
c++中std::find
find()作為STL函數 (find() as a STL function)
find() is an STL function that comes under the <algorithm> header file which returns an iterator to the first occurrence of the searching element within a range.
find()是STL函數,位于< algorithm>頭文件下面,該文件將迭代器返回到范圍內搜索元素的第一個匹配項。
Syntax:
句法:
InputIterator find(InputIterator first, InputIterator last, const T& val);Where,
哪里,
InputIterator first - iterator to start of the searching range
首先使用InputIterator-迭代器開始搜索范圍
InputIterator last - iterator to end of the searching range
InputIterator last-迭代到搜索范圍的結尾
const T& val - value to be searched of datatype T
const T&val-要搜索的數據類型T的值
What is InputIterator?
Iterator to first position of the range where we find the searching element. If searching element is not found it returns iterator to the end
什么是InputIterator?
迭代到找到搜索元素的范圍的第一個位置。 如果未找到搜索元素,則將迭代器返回到末尾
Return type: bool
返回類型: bool
Using the above syntax the elements in the corresponding ranges are searched whether the searching element is found.
使用以上語法,搜索相應范圍內的元素是否找到了搜索元素。
Time complexity: Linear time, O(n)
時間復雜度:線性時間,O(n)
Difference between binary_search() and find() functions
binary_search()和find()函數之間的區別
std::binary_search() function returns Boolean telling whether it finds or not. It doesn't return the position. But, std::find() searches the position too. It returns an iterator to the first position.
std :: binary_search()函數返回布爾值,指示是否找到。 它不返回位置。 但是,std :: find()也會搜索位置。 它將迭代器返回到第一個位置。
std::binary_search() searches in O(logn) time whether std::find() searches in linear time.
std :: binary_search()以O(logn)時間進行搜索,無論std :: find()以線性時間進行搜索。
Example 1: When the searched element is found and have only one instance in the searching range
示例1:當找到被搜索元素并且在搜索范圍內只有一個實例時
#include <bits/stdc++.h> using namespace std;int main() {vector<int> arr{ 1, 2, 3, 8, 4, 3 };int searching_element = 8;vector<int>::iterator it;//starting iterator of range= arr.begin()//end iterator of range =arr.end()it = find(arr.begin(), arr.end(), searching_element);if (it != arr.end())cout << searching_element << " is at position: " << it - arr.begin() << endl;elsecout << searching_element << "not found";return 0; }Output:
輸出:
8 is at position: 3In the above program, we have checked the searching element and we found it at 3rd index(0-indexing)
在上面的程序中,我們檢查了搜索元素,并在第三個索引(0索引)處找到了它
Example 2: When the searched element is found and have more than one instance in the searching range
示例2:找到搜索到的元素并且在搜索范圍內有多個實例
#include <bits/stdc++.h> using namespace std;int main() {vector<int> arr{ 1, 2, 3, 8, 4, 3 };int searching_element = 3;vector<int>::iterator it;//starting iterator of range= arr.begin()//end iterator of range =arr.end()it = find(arr.begin(), arr.end(), searching_element);if (it != arr.end())cout << searching_element << " is at position: " << it - arr.begin() << endl;elsecout << searching_element << "not found";return 0; }Output:
輸出:
3 is at position: 2In the above case, we are searching 3 in the array which has two instances one at position index 2 and the other is at position 5. Since std::find() stops searching whenever it finds the searching element thus it returns index 2 (Check how we found the position from the iterator it returned).
在上述情況下,我們在數組中搜索3,該數組在位置索引2上有兩個實例,另一個在位置5上。由于std :: find()每當找到搜索元素時就停止搜索,因此返回索引2(檢查我們如何從返回的迭代器中找到位置。
Example 3: When the searched element is not found in the searching range
示例3:在搜索范圍內未找到搜索元素時
#include <bits/stdc++.h> using namespace std;int main() {vector<int> arr{ 1, 2, 3, 8, 4, 3 };int searching_element = 7;vector<int>::iterator it;//starting iterator of range= arr.begin()//end iterator of range =arr.end()it = find(arr.begin(), arr.end(), searching_element);if (it != arr.end())cout << searching_element << " is at position: " << it - arr.begin() << endl;elsecout << searching_element << " not found";return 0; }Output:
輸出:
7 not foundIn the above case, the searching element is not present and that's why it returned arr.end() which means iterator to the end of the range.
在上述情況下,不存在search元素,這就是為什么它返回arr.end()的原因,這意味著迭代器將到達范圍的末尾。
翻譯自: https://www.includehelp.com/stl/std-find-with-examples-in-cpp.aspx
c++中std::find
總結
以上是生活随笔為你收集整理的c++中std::find_std :: find()与C ++中的示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: c语言 函数的参数传递示例_isgrea
- 下一篇: stl list 删除元素_删除所有出现