3

It's easy to find an item in a vector and we know how. It's also possible to find an item in an array if we know the length of the array. But starting from c++11 with the announcement of std:: end(), it looks like we can get the size of the array by calling its pointer, right?

So this code works:

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

std::find(begin(arr), end(arr), needle);
int* result = find(begin(arr), end(arr), needle);
bool exists = result!=end(arr);

However, if this converted to a function:

bool in_array(int arr[], int needle){
    int* result = std::find(begin(arr), end(arr), needle);
    return result!=end(arr);
}

Will not compile:

error: no matching function for call to ‘begin(int*&)’ int* result = std::find( std::begin(arr), std::end(arr), needle);

The goal is to make a working function to send any type of array to it, just like in_array of Php:

template <typename T>
T in_array(T arr[],T needle){
    T* result = std::find(begin(arr), end(arr), needle);
    return result!=end(arr);
}

Why does this code not work? Does C++ compiler store some kind of a local variable to determine the end of arr?

2 Answers 2

6

This line:

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

Doesn't create an array of type int[] but int[5]. You can check this by asking the size of the array, it will return sizeof(int)*5.

But for the function, you don't pass the size (see the error message after array decay), just a pointer, so there is no possibility to get end() for it.

What you can do is pass the size as well (not tested, but should be good):

template <typename T, int N>
bool in_array(T arr[N],T needle){
    T* result = std::find(begin(arr), end(arr), needle);
    return result!=end(arr);
}
Sign up to request clarification or add additional context in comments.

8 Comments

Maybe const T& needle? There shouldn't be any need to assume that copying an array element is inexpensive. Also, the return value should be bool.
@lubgr agreed for the return type (just copied OP's code), not for the const&.
@JafarAkhondali No, its not, have a look at overload #2 here
Ok I see, though I'd insist on const& as this is a template, but now it comes to nitpicking, you have my upvote anyway :)
I agreed, it's a matter of taste. I worked with enough people that don't know the difference that I cannot tell them in which situation to use one or the other and they don't mess up :( For my code, I may use const& as you suggested indeed.
|
1

error: no matching function for call to ‘begin(int*&)’ int* result = std::find( std::begin(arr), std::end(arr), needle);

having

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

the size/number of elements if known, but in

 bool in_array(int arr[], int needle){

the number of element is unknown, array is just known as a pointer to int

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.