I have a number and a vector;
How can I check if a number is an element of a vector?
Thank you
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
>Is there any way that I could do it without a loop or vector size?
Use find() from header <algorithm> like CornedBee suggested.