Are you sure you didn't forget to update the declaration at the top of your code?
int is a signed type (it can represent negative numbers).
vector_array.size() returns an unsigned type (that can not represent negative numbers).
Your compiler generates a warning because comparing signed and unsigned integers is error-prone. Before doing the comparison i < vector_array.size()
it will first convert the i
to an unsigned integer. In this case it's not a problem because the i
is never negative.
What I usually do in this situation is to use std::size_t which is an unsigned type.
for(std::size_t i = 0; i < vector_array.size(); i++)
It makes it a bit more clear that i
is never negative and it silence the error.
Another alternative if you want to keep using signed indices is to use std::ssize(vector_array)
instead of vector_array.size()
Some people prefer to use const whenever they can but personally I don't think there is much reason to use const here. I wouldn't personally do it.
It would have been important if the parameters were pointers or references because that would have affected what values the caller could pass to the function but since the arguments are "passed by value" (copied) it's essentially the same as marking a local variable as const (i.e. an implementation detail).
That said, I think it's a good idea to pass the vector and string by "const reference" in your situation to avoid making unnecessary copies.
int inputValidate(int, const string&);
bool linearSearch(const vector<int>&, int);
If you don't use const here then the caller would not be able to pass temporaries (e.g. string literals) and objects declared as const.