I have a simple function in C++ that checks whether a given element exists in a given array and return true
or false
accordingly. Here is an example of the code and how I am trying to use it:
#include <iostream>
using namespace std;
bool find_elem(string array[], string elem)
{
return (find(begin(array), end(array), elem) != end(array));
}
int main(void)
{
string names[] = {"Bob", "Bill", "Sally", "Mark"}
if (find_elem(names, "Bob))
{
cout << "Bob is in the array" << endl;
}
else
{
cout << "Bob is not in the array" << endl;
}
return 0;
}
I am getting an error message on the return statement for the find_elem()
function saying clang++: error - no matching function for call to 'begin'
as well as two more that say the same for end
. I am not sure why this is happening because this line of code works just fine in the main()
function of my program, but if I try to use it anywhere else, it returns an error. Can somebody explain why this is happening and how I might fix it? I only recently started learning C++ so examples or suggestions for how to do this better would be more than welcome.