C言語のfind()関数

4702 ワード

find関数は、配列内の指定された要素の位置を検索するために使用されます.
例えば、[0,0,5,4]の配列があります.問:要素5がどこにあるか、find関数の戻り値は2です.
find(配列名+検索開始要素の位置、配列名+検索終了要素の位置、検索したい要素)
直接コード:
#include 
#include 
#include //         
using namespace std;
int main()
{
    int nums[] = { 3, 1, 4, 1, 5, 9 };
    int num_to_find = 5;
    int start = 0;
    int end = 5;
    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;
    }
    return 0;
}
--------------------- 
  :hyperminer 
  :CSDN 
  :https://blog.csdn.net/zhangweijiqn/article/details/9107571 
    :       hyperminer     。