0
#include <iostream>

using namespace std;

int vowels[] = {65, 69, 73, 79, 85, 97, 101, 105, 111, 117}; // ASCII codes (upper & lower case).

bool characterInArray(char letter, int arr[]) {
  return find(begin(arr), end(arr), (int) letter) != end(arr);
}

int main() {
  if (characterInArray('i', vowels)) {
    cout << "Found a vowel in the character provided." << endl;
  }
  return 0;
}

Given this code, I'm given an error "error: no matching function for call to "begin". The full error message is below, what am I doing wrong here?

candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'int *'
2
  • 1
    Passing array as function parameter -- In your code, you are actually passing a pointer to the first element, not the array. So your function is no different than bool characterInArray(char letter, int *arr). That should explain the error you're getting. Commented Feb 9, 2018 at 22:57
  • Instead of using numbers like 65, use 'A' etc. It's much clearer. Commented Feb 9, 2018 at 23:00

1 Answer 1

3

In C++, a function parameter like int arr[] is adjusted to int* arr, which is why begin() and end() don't work. You can solve this by passing a reference to an array const int (&arr)[10], but it may be simpler to drop the function and just call any_of with the right predicate,

if (any_of(begin(vowels), end(vowels),
    [](char c) { return c == 'i'; }) { ... }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.