rectangletangle
rectangletangle

Reputation: 53051

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

Pseudo Code:

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

x = find(3).arr ; 

x would then return 2.

Upvotes: 31

Views: 196035

Answers (9)

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

Upvotes: -1

pm100
pm100

Reputation: 50210

The fancy answer:

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

The simple answer

Use a for loop

Upvotes: 4

david.omego
david.omego

Reputation: 11

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

Upvotes: 1

rashedcs
rashedcs

Reputation: 3725

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

Upvotes: 2

Nightswatch
Nightswatch

Reputation: 143

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

Upvotes: 3

virious
virious

Reputation: 571

#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.

Upvotes: 2

Peter Alexander
Peter Alexander

Reputation: 54300

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

Upvotes: 59

Brian
Brian

Reputation: 25834

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

Upvotes: 15

andand
andand

Reputation: 17507

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

Upvotes: 2

Related Questions