Is some value in a vector

Hi I am trying to make an if statement that asks if a number is a member of a vector but am not sure how to do this? I want something of the form:

if(number is an entry in vector_name) do whatever;

Thank you
Try the find() function.
http://cplusplus.com/reference/algorithm/find/

You'd probably end up with something like:
std::find(vector_name.begin(), vector_name.end(), number)
You might look at input iterators such as find().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
  std::vector<int> myvector;
  myvector.push_back(12);
  myvector.push_back(23);
  myvector.push_back(52);
  myvector.push_back(1);

  int key = 2;

  if ( std::find (myvector.begin(), myvector.end(), key) != myvector.end() )
    std::cout << "FOUND IT!" << std::endl;

  return 0;
}


Easy.
Good job Zhuge and wolfgang!
I got it now, thanks all
Topic archived. No new replies allowed.