std::find on a double array

Dec 11, 2015 at 4:32pm
I have a double array double myArray[] and a scalar x. I would like to check if x is an element of myArray[]. I already know how to do this by std::find if the array and the scalar are both integers. e.g.

1
2
3
4
5
6
7
8
9
10
#include <algorithm>

int myArray[] = { 3, 2, 1, 0, 1, 2, 3 };
size_t myArraySize = sizeof(myArray) / sizeof(int);
int *end = myArray + myArraySize;
// find the value 0:
int *result = std::find(myArray, end, 0);
if (result != end) {
  // found value at "result" pointer location...
}


I have a very large number of arrays to check and do not prefere to convert every element in loop. Is there any efficient way around here without looping over the array?

Thansk for any help
Last edited on Dec 11, 2015 at 4:34pm
Dec 11, 2015 at 7:49pm
> without looping over the array?
¿what do you think that std::find() does?


> I already know how to do this by std::find if the array and the scalar are both integers
¿why would there be any difference?
once you respond that http://www.cplusplus.com/reference/algorithm/find_if/
Dec 11, 2015 at 9:02pm
Don't think there is a way to directly access the index where "x" is found, unless you implement a separate counter and your array is sorted

correct me if im not mistaken but doesnt std::find also perform a loop?
Dec 11, 2015 at 10:02pm
I am quite beginner, but I guess yes. I guess I have to write my own find. Thanks for the time.
Last edited on Dec 11, 2015 at 10:03pm
Dec 11, 2015 at 10:17pm
You can calculate the index by subtracting the pointer to the first element in the array from the pointer returned by std::find.

 
int index = result - myArray;
Topic archived. No new replies allowed.