I have a number and a vector;

How can I check if a number is an element of a vector?

Thank you

Printable View

- 02-04-2005strickeyVector elements
I have a number and a vector;

How can I check if a number is an element of a vector?

Thank you - 02-04-2005CornedBee
Use the std::find algorithm.

- 02-04-2005swoopyCode:
`for (int i=0; i<vec.size(); ++i)`

{

if (vec[i] == number)

{

cout << number << " is an element of vector." << endl;

break;

}

}

- 02-04-2005strickeyno vector size
Is there any way that I could do it without a loop or vector size?

- 02-04-2005CornedBee
Not really. My method avoids calling size() and has no visible loop, but it's just hidden.

- 02-04-2005swoopy
>Is there any way that I could do it without a loop or vector size?

Use find() from header <algorithm> like CornedBee suggested. ;)