Home > Web Front-end > JS Tutorial > How to find an element in an array using find function?

How to find an element in an array using find function?

WBOY
Release: 2023-11-18 12:24:00
Original
1803 people have browsed it

How to find an element in an array using find function?

In the programming process, there are many times when we need to find an element in an array. At this time, we can use the find function to easily complete this task. This article details how to use the find function to find elements in an array and provides some code examples.

1. Definition of find function

In C STL, the find function is defined in the header file and has the following two overloaded forms:

  1. find (iterator first, iterator last, element value to be found): Find value in the range [first, last) and return the iterator pointing to the element. If not found, return iterator last.
  2. find(iterator first, iterator last, element value to be found, function cmp): Same as above, but use function cmp to determine whether the elements are equal.

Among them, the second version of the cmp function needs to meet the following requirements:

  • The function return value is bool type;
  • The function parameter type is the same as the one to be used. Find elements with the same value.

2. Use the find function to find elements in the array

Suppose we have an integer array a and want to find whether it contains element x. We can use the find function to complete this task. The specific code is as follows:

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int a[] = {1, 3, 5, 7, 9};
    int x = 5;
    int* p = find(a, a + 5, x);    // 在a数组中查找元素x
    if (p != a + 5) {    // 如果找到了,输出对应下标,否则输出未找到
        cout << "找到了,下标为:" << p - a << endl;
    } else {
        cout << "未找到" << endl;
    }
    return 0;
}
Copy after login

The above code first defines an integer array a and an element x to be found, and then uses the find function to find the element x in the array a. If found, output the subscript of the element in the array; otherwise, output not found.

3. Use function objects to customize comparison rules

The second version of the find function allows the use of function objects to customize comparison rules. In the following example, we define a struct Cmp and overload the () operator to compare the size according to the absolute size of the elements.

#include <iostream>
#include <algorithm>
using namespace std;

struct Cmp {
    bool operator() (int x, int y) {
        return abs(x) < abs(y);
    }
};

int main() {
    int a[] = {-3, 1, -5, 7, 2};
    int x = -5;
    int* p = find(a, a + 5, x, Cmp());    // 在a数组中按照绝对值大小查找元素x
    if (p != a + 5) {
        cout << "找到了,下标为:" << p - a << endl;
    } else {
        cout << "未找到" << endl;
    }
    return 0;
}
Copy after login

In the above code, we define the comparison rule as a structure named Cmp, which overloads the () operator so that it compares the absolute value of the elements. In the find function, we use Cmp() to instantiate a function object to search according to the defined rules.

Summary

This article explains how to use the find function in C STL to find elements in an array and provides some code examples. By customizing the comparison rules, we also demonstrate the flexibility of the find function. In actual development, we can refer to the content of this article to choose appropriate comparison rules to provide better functions for our programs.

The above is the detailed content of How to find an element in an array using find function?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template