I have a number and a vector;
How can I check if a number is an element of a vector?
Thank you
Printable View
I have a number and a vector;
How can I check if a number is an element of a vector?
Thank you
Use the std::find algorithm.
Code:for (int i=0; i<vec.size(); ++i)
{
if (vec[i] == number)
{
cout << number << " is an element of vector." << endl;
break;
}
}
Is there any way that I could do it without a loop or vector size?
Not really. My method avoids calling size() and has no visible loop, but it's just hidden.
>Is there any way that I could do it without a loop or vector size?
Use find() from header <algorithm> like CornedBee suggested. ;)