i was making a programme in which i had a vector<string> i need to return the string in this vector having longest length so i simply write this loop
1 2 3 4 5
int max=-1;
for(int i=0;i<v.size();i++){
if(v[i].size()>max){ max=v[i].size(); sol=v[i];}
}
return sol;
but is was not entering in the if statement
thn i tried this statement
int num=-1; if(v[0].size()>num) cout<<"we";
and it gives this error "invalid use of member (did you forget the ‘&’ "
can anyon please tell me where is the error
return type of v[i].size() is int and i am simply comparing it with int.where is the problem??
vector<>::size() returns a vector<>::size_type, which might be a size_t.
However, OP's code was v[0].size(). This is attempting to call size() on string.
The correct function call is v[0].length(). Note that string::length() returns a
string::size_type.