How to get the index number of an element into an array?

Nov 23, 2014 at 10:53am
I tried to use find() function but it returns the element not the position.

1
2
3
4
5
int ar[5]={1,2,3,4,3};

int *l;
l=find(ar, ar+5, 3);
cout<<*l<<endl;


is there any function that returns the position?
Last edited on Nov 23, 2014 at 10:54am
Nov 23, 2014 at 11:06am
your information is not detail...
what result you expected for...?
Nov 23, 2014 at 11:07am
Yes. find returns an iterator to that position.
To get the index, subtract the value returned from the beginning of the array.
1
2
l = find(ar, ar + 5, 3);
cout<<"position is "<<l-ar<<enld;
Topic archived. No new replies allowed.