-1
vector<int> v(n);

Given a vector of n randomly generated numbers, I want to set a random variable and determine if that variable is located in that vector using std::find algorithm.

int x = rand();
int p = std::find(v.begin(), v.end(), x); 

When I first run this code, I get an error along the lines of "cannot convert iterator to int in initialization".

Tenquen
  • 13
  • 1

1 Answers1

2

Excerpt from the documentation

Return value

Iterator to the first element satisfying the condition or last if no such element is found.

To satisfy that using the example from the documentation

auto p = std::find(v.begin(), v.end(), x);
bool found = (p != v.end());

Found now contains if the value you mentioned was found or not.

Samer Tufail
  • 1,835
  • 15
  • 25