C语言 如果给定值,则查找数组索引
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25003961/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find array index if given value
提问by user1798299
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 函数。例如,数组[2] = {4, 7, 8}。我知道值是 7,如果我知道它在数组 [1] 处,我如何获得索引的值 1?
回答by Vlad from Moscow
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.
您也可以使用 int 类型代替 size_t 类型。
But the better way is to use standard algorithm std::findor std::find_ifdeclared in header <algorithm>provided that you use C++
但更好的方法是使用标准算法std::find或std::find_if在标头中声明,<algorithm>前提是您使用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.:)
否则你必须像我上面展示的那样自己编写函数。:)
If the array is sorted you can use standard C function bsearchdeclared in header <stdlib.h>
如果数组已排序,则可以使用标bsearch头中声明的标准 C 函数<stdlib.h>
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;
}
回答by ryyker
First its important that the argument list contain size information for the array, i.e. passing a pointer to an arrayonlydoes 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
对于小型阵列,这种方法会很好。对于非常大的数组,一些排序和二分搜索会提高性能

