suppose there is a vector whose size is 0. So if we write cout<<ans.size()-1 does it not output -1?
A random question
@meadri17, that’s an interesting question , but it does return -1, but not in the way you would expect, i am sure you might have done cout<<ans.size() -1<<endl; you might got some crazy output but there is no problem with the output , actually what happens is vector.size() return a unsigned number and you might know that unsigned integers only store positive numbers,so what happens is - operator gets overloaded and returns a unsigned number, so that crazy number is an attempt of your computer to have -1 as a unsigned integer which is a garbage value
you can test it by doing
unsigned int size=0;
cout<<size-1<<endl;
it will return the same crazy number as above
so what you need to do is print like this:-
cout<<(int)(ans.size())-1<<endl;
here you have typecasted ans.size() to a signed integer and - operator will then return a signed integer as output as -1 can be represented in signed
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.