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;
    else
        cout << searching_element << "not found";

    return 0;
}

Output:

输出:

8 is at position: 3

In 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;
    else
        cout << searching_element << "not found";

    return 0;
}

Output:

输出:

3 is at position: 2 

In 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;
    else
        cout << searching_element << " not found";

    return 0;
}

Output:

输出:

7 not found

In 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值