繁体   English   中英

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

[英]How do I find a particular value in an array and return its index?

伪代码:

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

x = find(3).arr ; 

x 然后会返回 2。

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

要查找索引,请使用<algorithm>标头中的std::distancestd::find

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

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

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;
}

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

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

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

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

#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;    
}

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

简单的答案

使用for循环

#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;
}

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

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();

我们这里只使用线性搜索。 首先将索引初始化为-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;
 }

有一个find(...) function 用于查找数组中的元素,该元素返回指向该元素的迭代器。 如果未找到该元素,则迭代器指向数组末尾。

如果找到元素,我们可以简单地计算迭代器与数组开头的距离,以获得该元素的索引。

using namespace std;

int arr[ 5 ] = { 4, 1, 3, 2, 6 }
auto it = arr.find(begin(arr), end(arr), 3)

if(it != end(arr))
    cerr << "Found at index: " << (it-begin(arr)) << endl;
else
    cerr << "Not found\n";

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

您可以使用提供的 STL 算法库的查找功能

#include <iostream>
#include <algorithm>


using std::iostream;
using std::find;

int main() {
  int length = 10;
  int arr[length] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  
  int* found_pos = find(arr, arr + length, 5);
  
  if(found_pos != (arr + length)) {
    // found
    cout << "Found: " << *found_pos << endl;
  }
  else {
    // not found
    cout << "Not Found." << endl;
  }
  
  
  return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM