如果给定值,则查找数组索引

时间:2022-03-24 21:27:02

I want to retrieve the index in the array where the value is stored. I know the value of the item at that point in the array. I'm thinking it's similar to the findIndex function in c#. For example, array[2] = {4, 7, 8}. I know the value is 7, how do I get the value of the index, 1, if I know it is at array[1]?

我想检索存储值的数组中的索引。我知道数组中该项的值。我认为它类似于c#中的findIndex函数。例如,array [2] = {4,7,8}。我知道值是7,如果我知道它在数组[1],我怎么得到索引的值1?

2 个解决方案

#1


7  

For example you can define the corresponding function the following way

例如,您可以通过以下方式定义相应的功能

size_t FindIndex( const int a[], size_t size, int value )
{
    size_t index = 0;

    while ( index < size && a[index] != value ) ++index;

    return ( index == size ? -1 : index );
}

Also instead of type size_t you can use type int.

而不是类型size_t,您可以使用int类型。

But the better way is to use standard algorithm std::find or std::find_if declared in header <algorithm> provided that you use C++

但更好的方法是使用在标头 中声明的标准算法std :: find或std :: find_if,前提是您使用C ++

For example

#include <algorithm>
#include <iterator>

int main()
{
    int a[] = { 4, 7, 8 };

    auto it = std::find( std::begin( a ), std::end( a ), 7 );

    if ( it != std::end( a ) )
    {
        std::cout << "The index of the element with value 7 is " 
                  << std::distance( std::begin( a ), it )
                  << std::endl;
    }
} 

The output is

输出是

The index of the element with value 7 is 1

Otherwise you have to write the function yourself as I showed abve.:)

否则你必须在我展示abve时自己编写这个函数。:)

If the array is sorted you can use standard C function bsearch declared in header <stdlib.h>

如果数组已排序,则可以使用标头 中声明的标准C函数bsearch

For example

#include <stdio.h>
#include <stdlib.h>


int cmp( const void *lhs, const void *rhs )
{
    if ( *( const int * )lhs < *( const int * )rhs ) return -1;
    else if ( *( const int * )rhs < *( const int * )lhs ) return 1;
    else return 0;
}

int main() 
{
    int a[] = { 4, 7, 8 };

    int x = 7;
    int *p  = ( int * )bsearch( &x, a, 3, sizeof( int ), cmp );

    if ( p != NULL ) printf( "%d\n", p - a );

    return 0;
}

#2


0  

First its important that the argument list contain size information for the array, i.e. passing a pointer to an array only does not provide enough information to know how many elements the array has. The argument decays into a pointer type with no size information to the function.

首先,重要的是参数列表包含数组的大小信息,即仅将指针传递给数组不能提供足够的信息来知道数组有多少元素。参数衰减为指针类型,函数没有大小信息。

So given that, you could do something like this:

所以,鉴于此,您可以这样做:

int findIndex(int *array, size_t size, int target) 
{
    int i=0;
    while((i<size) && (array[i] != target)) i++;

    return (i<size) ? (i) : (-1);
}

For small arrays this approach will be fine. For very large arrays, some sorting and a binary search would improve performance

对于小阵列,这种方法很好。对于非常大的数组,一些排序和二进制搜索将提高性能

#1


7  

For example you can define the corresponding function the following way

例如,您可以通过以下方式定义相应的功能

size_t FindIndex( const int a[], size_t size, int value )
{
    size_t index = 0;

    while ( index < size && a[index] != value ) ++index;

    return ( index == size ? -1 : index );
}

Also instead of type size_t you can use type int.

而不是类型size_t,您可以使用int类型。

But the better way is to use standard algorithm std::find or std::find_if declared in header <algorithm> provided that you use C++

但更好的方法是使用在标头 中声明的标准算法std :: find或std :: find_if,前提是您使用C ++

For example

#include <algorithm>
#include <iterator>

int main()
{
    int a[] = { 4, 7, 8 };

    auto it = std::find( std::begin( a ), std::end( a ), 7 );

    if ( it != std::end( a ) )
    {
        std::cout << "The index of the element with value 7 is " 
                  << std::distance( std::begin( a ), it )
                  << std::endl;
    }
} 

The output is

输出是

The index of the element with value 7 is 1

Otherwise you have to write the function yourself as I showed abve.:)

否则你必须在我展示abve时自己编写这个函数。:)

If the array is sorted you can use standard C function bsearch declared in header <stdlib.h>

如果数组已排序,则可以使用标头 中声明的标准C函数bsearch

For example

#include <stdio.h>
#include <stdlib.h>


int cmp( const void *lhs, const void *rhs )
{
    if ( *( const int * )lhs < *( const int * )rhs ) return -1;
    else if ( *( const int * )rhs < *( const int * )lhs ) return 1;
    else return 0;
}

int main() 
{
    int a[] = { 4, 7, 8 };

    int x = 7;
    int *p  = ( int * )bsearch( &x, a, 3, sizeof( int ), cmp );

    if ( p != NULL ) printf( "%d\n", p - a );

    return 0;
}

#2


0  

First its important that the argument list contain size information for the array, i.e. passing a pointer to an array only does not provide enough information to know how many elements the array has. The argument decays into a pointer type with no size information to the function.

首先,重要的是参数列表包含数组的大小信息,即仅将指针传递给数组不能提供足够的信息来知道数组有多少元素。参数衰减为指针类型,函数没有大小信息。

So given that, you could do something like this:

所以,鉴于此,您可以这样做:

int findIndex(int *array, size_t size, int target) 
{
    int i=0;
    while((i<size) && (array[i] != target)) i++;

    return (i<size) ? (i) : (-1);
}

For small arrays this approach will be fine. For very large arrays, some sorting and a binary search would improve performance

对于小阵列,这种方法很好。对于非常大的数组,一些排序和二进制搜索将提高性能