如何在数组中找到特定值并返回其索引?

时间:2023-01-22 07:21:13

Pseudo Code:

伪代码:

int arr[ 5 ] = { 4, 1, 3, 2, 6 }, x;

x = find(3).arr ; 

x would then return 2.

然后x将返回2。

7 个解决方案

#1


35  

The syntax you have there for your function doesn't make sense (why would the return value have a member called arr?).

你的函数的语法没有意义(为什么返回值有一个名为arr的成员?)。

To find the index, use std::distance and std::find from the <algorithm> header.

要查找索引,请使用 标头中的std :: distance和std :: find。

int x = std::distance(arr, std::find(arr, arr + 5, 3));

Or you can make it into a more generic function:

或者你可以把它变成一个更通用的功能:

template <typename Iter>
size_t index_of(Iter first, Iter last, typename const std::iterator_traits<Iter>::value_type& x)
{
    size_t i = 0;
    while (first != last && *first != x)
      ++first, ++i;
    return i;
}

Here, I'm returning the length of the sequence if the value is not found (which is consistent with the way the STL algorithms return the last iterator). Depending on your taste, you may wish to use some other form of failure reporting.

在这里,如果找不到值,我将返回序列的长度(这与STL算法返回最后一个迭代器的方式一致)。根据您的喜好,您可能希望使用其他形式的故障报告。

In your case, you would use it like so:

在你的情况下,你会像这样使用它:

size_t x = index_of(arr, arr + 5, 3);

#2


10  

Here is a very simple way to do it by hand. You could also use the <algorithm>, as Peter suggests.

这是一种非常简单的手工操作方法。彼得建议你也可以使用

#include <iostream>
int find(int arr[], int len, int seek)
{
    for (int i = 0; i < len; ++i)
    {
        if (arr[i] == seek) return i;
    }
    return -1;
}
int main()
{
    int arr[ 5 ] = { 4, 1, 3, 2, 6 };
    int x = find(arr,5,3);
    std::cout << x << std::endl;    
}

#3


3  

the fancy answer. Use std::vector and search with std::find

花哨的答案。使用std :: vector并使用std :: find进行搜索

the simple answer

简单的答案

use a for loop

使用for循环

#4


2  

#include <vector>
#include <algorithm>

int main()
{
     int arr[5] = {4, 1, 3, 2, 6};
     int x = -1;
     std::vector<int> testVector(arr, arr + sizeof(arr) / sizeof(int) );

     std::vector<int>::iterator it = std::find(testVector.begin(), testVector.end(), 3);
     if (it != testVector.end())
     {
          x = it - testVector.begin();
     }
     return 0;
}

Or you can just build a vector in a normal way, without creating it from an array of ints and then use the same solution as shown in my example.

或者你可以以正常方式构建一个向量,而不是从一个int数组创建它,然后使用我的例子中所示的相同解决方案。

#5


1  

int arr[5] = {4, 1, 3, 2, 6};
vector<int> vec;
int i =0;
int no_to_be_found;

cin >> no_to_be_found;

while(i != 4)
{
    vec.push_back(arr[i]);
    i++;
}

cout << find(vec.begin(),vec.end(),no_to_be_found) - vec.begin();

#6


0  

If the array is unsorted, you will need to use linear search.

如果数组未排序,则需要使用线性搜索。

#7


0  

We here use simply linear search. At first initialize the index equal to -1 . Then search the array , if found the assign the index value in index variable and break. Otherwise, index = -1.

我们这里只使用线性搜索。首先将索引初始化为-1。然后搜索数组,如果找到则在索引变量中分配索引值并中断。否则,index = -1。

   int find(int arr[], int n, int key)
   {
     int index = -1;

       for(int i=0; i<n; i++)
       {
          if(arr[i]==key)
          {
            index=i;
            break;
          }
       }
      return index;
    }


 int main()
 {
    int arr[ 5 ] = { 4, 1, 3, 2, 6 };
    int n =  sizeof(arr)/sizeof(arr[0]);
    int x = find(arr ,n, 3);
    cout<<x<<endl;
    return 0;
 }

#1


35  

The syntax you have there for your function doesn't make sense (why would the return value have a member called arr?).

你的函数的语法没有意义(为什么返回值有一个名为arr的成员?)。

To find the index, use std::distance and std::find from the <algorithm> header.

要查找索引,请使用 标头中的std :: distance和std :: find。

int x = std::distance(arr, std::find(arr, arr + 5, 3));

Or you can make it into a more generic function:

或者你可以把它变成一个更通用的功能:

template <typename Iter>
size_t index_of(Iter first, Iter last, typename const std::iterator_traits<Iter>::value_type& x)
{
    size_t i = 0;
    while (first != last && *first != x)
      ++first, ++i;
    return i;
}

Here, I'm returning the length of the sequence if the value is not found (which is consistent with the way the STL algorithms return the last iterator). Depending on your taste, you may wish to use some other form of failure reporting.

在这里,如果找不到值,我将返回序列的长度(这与STL算法返回最后一个迭代器的方式一致)。根据您的喜好,您可能希望使用其他形式的故障报告。

In your case, you would use it like so:

在你的情况下,你会像这样使用它:

size_t x = index_of(arr, arr + 5, 3);

#2


10  

Here is a very simple way to do it by hand. You could also use the <algorithm>, as Peter suggests.

这是一种非常简单的手工操作方法。彼得建议你也可以使用

#include <iostream>
int find(int arr[], int len, int seek)
{
    for (int i = 0; i < len; ++i)
    {
        if (arr[i] == seek) return i;
    }
    return -1;
}
int main()
{
    int arr[ 5 ] = { 4, 1, 3, 2, 6 };
    int x = find(arr,5,3);
    std::cout << x << std::endl;    
}

#3


3  

the fancy answer. Use std::vector and search with std::find

花哨的答案。使用std :: vector并使用std :: find进行搜索

the simple answer

简单的答案

use a for loop

使用for循环

#4


2  

#include <vector>
#include <algorithm>

int main()
{
     int arr[5] = {4, 1, 3, 2, 6};
     int x = -1;
     std::vector<int> testVector(arr, arr + sizeof(arr) / sizeof(int) );

     std::vector<int>::iterator it = std::find(testVector.begin(), testVector.end(), 3);
     if (it != testVector.end())
     {
          x = it - testVector.begin();
     }
     return 0;
}

Or you can just build a vector in a normal way, without creating it from an array of ints and then use the same solution as shown in my example.

或者你可以以正常方式构建一个向量,而不是从一个int数组创建它,然后使用我的例子中所示的相同解决方案。

#5


1  

int arr[5] = {4, 1, 3, 2, 6};
vector<int> vec;
int i =0;
int no_to_be_found;

cin >> no_to_be_found;

while(i != 4)
{
    vec.push_back(arr[i]);
    i++;
}

cout << find(vec.begin(),vec.end(),no_to_be_found) - vec.begin();

#6


0  

If the array is unsorted, you will need to use linear search.

如果数组未排序,则需要使用线性搜索。

#7


0  

We here use simply linear search. At first initialize the index equal to -1 . Then search the array , if found the assign the index value in index variable and break. Otherwise, index = -1.

我们这里只使用线性搜索。首先将索引初始化为-1。然后搜索数组,如果找到则在索引变量中分配索引值并中断。否则,index = -1。

   int find(int arr[], int n, int key)
   {
     int index = -1;

       for(int i=0; i<n; i++)
       {
          if(arr[i]==key)
          {
            index=i;
            break;
          }
       }
      return index;
    }


 int main()
 {
    int arr[ 5 ] = { 4, 1, 3, 2, 6 };
    int n =  sizeof(arr)/sizeof(arr[0]);
    int x = find(arr ,n, 3);
    cout<<x<<endl;
    return 0;
 }