【问题标题】:How do I find a particular value in an array and return its index?如何在数组中找到特定值并返回其索引?
【发布时间】:2010-10-11 20:40:07
【问题描述】:

伪代码:

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

x = find(3).arr ; 

x 会返回 2。

【问题讨论】:

  • 索引至少应该是无符号类型。
  • 您确定要使用 index.使用某种形式的迭代器可能会更整洁。
  • 如果搜索到的值没有找到,你要什么返回值?

标签: c++ arrays


【解决方案1】:

你的函数的语法没有意义(为什么返回值会有一个名为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);

【讨论】:

    【解决方案2】:

    这是一种非常简单的手动操作方法。您也可以按照 Peter 的建议使用 &lt;algorithm&gt;

    #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】:

      花哨的答案:

      使用std::vector 并使用std::find 进行搜索

      简单的答案

      使用forloop

      【讨论】:

      • 最佳答案:在数组上使用std::find()
      【解决方案4】:
      #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;
      }
      

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

      【讨论】:

        【解决方案5】:

        如果数组未排序,则需要使用 linear search

        【讨论】:

          【解决方案6】:
          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();
          

          【讨论】:

          • 请为您的答案添加一些解释,以使其对其他读者更有用。
          【解决方案7】:

          我们在这里使用简单的线性搜索。首先初始化索引等于 -1 。然后搜索数组,如果找到在索引变量中分配索引值并中断。否则,索引 = -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;
           }
          

          【讨论】:

            【解决方案8】:

            您可以使用 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;
            }
            
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-09-16
              • 1970-01-01
              • 1970-01-01
              • 2016-01-14
              • 2016-10-23
              • 1970-01-01
              相关资源
              最近更新 更多