27

Pseudo Code:

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

x = find(3).arr ; 

x would then return 2.

Teemu Leisti
  • 3,750
  • 2
  • 30
  • 39
rectangletangle
  • 50,393
  • 94
  • 205
  • 275

9 Answers9

56

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

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

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.

In your case, you would use it like so:

size_t x = index_of(arr, arr + 5, 3);
Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
15

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;    
}
Brian
  • 25,523
  • 18
  • 82
  • 173
4

The fancy answer:

Use std::vector and search with std::find

The simple answer

Use a for loop

Codigo de Senior
  • 152
  • 1
  • 10
pm100
  • 48,078
  • 23
  • 82
  • 145
3
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();
Exa
  • 4,020
  • 7
  • 43
  • 60
Nightswatch
  • 133
  • 1
  • 8
2

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

andand
  • 17,134
  • 11
  • 53
  • 79
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.

virious
  • 571
  • 1
  • 8
  • 27
2

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.

   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;
 }
rashedcs
  • 3,588
  • 2
  • 39
  • 40
1

You could use the STL algorithm library's find function provided

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

david.omego
  • 11
  • 1
  • 3
0

There is a find(...) function to find an element in an array which returns an iterator to that element. If the element is not found, the iterator point to the end of array.

In case the element is found, we can simply calculate the distance of the iterator from the beginning of the array to get the index of that element.

#include <iterator>
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";
  • 1
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – Mark Rotteveel May 08 '22 at 11:57
  • What language is this? It's not valid C++. C-style arrays such as `int arr[5]` do not have member functions. – Jonathan Lidbeck Feb 07 '23 at 19:38
  • @JonathanLidbeck It is a valid and verified code. begin(array) is defined in library. – Arghavan Mohammadhassani Feb 08 '23 at 16:12
  • @Arghavan Mohammadhassani, you probably need just `find` instead of `arr.find`, since `int arr[5]` is a C-style array and doesn't have a method `find`. – Jonathan Lidbeck Mar 31 '23 at 01:49