0

I am trying to make a bool function which finds a value in an array and return true if it finds the value and or false if it doesn't, but I keep getting the error "No matching function to call to". Here's my code:

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <array>

using namespace std;
bool exists(int v[],int k)
{
    if(find(begin(v),end(v),k)!=end(v))
        return true;
    else
        return false;
}
int main()
{
    int v[5]={1,2,3,4,5};
    int k=4;
    if(exists(v,k))
        cout<<"yes";
    else cout<<"no";
    
    return 0;
}

What can I do to make it work?

scohe001
  • 13,879
  • 2
  • 28
  • 47
  • Not related to your problem, but whenever you see `if(thing) { return true; } else { return false; }`, you can just `return thing;`. So your whole `exists()` function can be condensed to `return find(begin(v), end(v), k) != end(v);` without changing the behavior at all. – scohe001 Jan 14 '21 at 21:03
  • This functions, begin and end, are for containers. You can use array conteiner for your v. Or you can use find function like this: if( find(v, v+5, k) != v+5). It is ugly, but works too. – Arseniy Jan 14 '21 at 21:07
  • 1
    Just seems odd to me that you've included the `` and `` headers but not used either of those containers. – Adrian Mole Jan 14 '21 at 21:08

0 Answers0