### krngrvr09's blog

By krngrvr09, history, 4 years ago,
vector<int> v;
cout<<v.size()-1;


The following will give you 18446744073709551615. This is because vector.size() returns a size_t type value, which is an alias for unsigned long int. In plain terms:

unsigned long int a=0;
cout<<a-1;


The above code will give the same result — 18446744073709551615.

### Implications

for(int i=0;i<vector.size()-1;i++){
...
}


In the above for loop, if the vector has 0 elements, then the loop will be equivalent to:

for(int i=0;i<18446744073709551615;i++){
...
}


Which, I believe is not what you will be expecting. So the correct way to use for loops is the following:

for(int i=0;(i+1)<vector.size();i++){
...
}


---Edit---

As pointed out by Errichto and satyaki3794, we can just typecast vector.size() to an int, and use it normally. Like this:

for(int i=0;i<(int)vector.size()-1;i++){
...
}


• +4

 » 4 years ago, # |   0 Auto comment: topic has been updated by krngrvr09 (previous revision, new revision, compare).
 » 4 years ago, # |   +16 easier to read: for(int i = 0; i < (int) vector.size() - 1; i++) { } 
•  » » 4 years ago, # ^ |   0 Yes, that makes more sense! I'll update the post! :D
 » 4 years ago, # |   0 You can just typecast vector.size() to an int instead. Then it will always give the correct value.
•  » » 4 years ago, # ^ |   0 Yes, that's better. I'll update the post. :D
 » 4 years ago, # |   -17 #define sz(a) (int)a.size() // you'll never forgot to typecast :) 
•  » » 4 years ago, # ^ |   0 #define sz(a) (int)(a).size() 
 » 4 years ago, # | ← Rev. 2 →   -7 for (int i = 0; i < vector.size() - 1; i++) { } => for (size_t i = 1; i < vector.size(); i++) { /* use i-1 */ } Because it's not a good idea to use signed number, when there is no need to use negative values.
•  » » 4 years ago, # ^ |   0 Suppose I need to iterate backward. I do not think making size_t unsigned was a good idea
•  » » » 4 years ago, # ^ |   0 That is a matter of taste. I prefer to use the following: for (size_t i = a.size(); i > 0; i--) { /* use i-1 */ } 
•  » » » » 4 years ago, # ^ |   +15 That's unreadable. Really
•  » » » » 4 years ago, # ^ | ← Rev. 3 →   0 you really suppose using i - 1 superior?I would understand if you've said for (size_t i = a.size(); i-- > 0;) but it's still not really readable
 » 7 weeks ago, # |   0 Why does the vector.size() give correct answer when the vector has non zero elements?
•  » » 7 weeks ago, # ^ |   0 vector.size() always gives a correct answer. If you have an empty vector, it will return size = 0. However, since it returns the answer in an unsigned format, if you minus one from 0, it overflows since unsigned numbers can't be negative.
•  » » » 7 weeks ago, # ^ |   0 So when overflows the output will be maximum value of unsigned long long?
•  » » » » 7 weeks ago, # ^ |   0 Yeah.
•  » » » » » 7 weeks ago, # ^ |   0 Can you give more insights via bit level calculations ?
•  » » » » » » 7 weeks ago, # ^ |   +10 Signed numbers use the first bit to tell if the number is negative or positive, while unsigned numbers use the first bit like any other bit. Ex. 101 would be 5 for an unsigned number, but -3 for a signed number. Integers use a system called two's complement to store negative numbers. Essentially how the system works is if I have a number N, -N = (reverse all bits of N) + 1. So when an unsigned long long number tries to do this with -1, it flips all the bits, changing the number like so: (binary representation of 1 = 0.....001) -> (1.....110). When you add 1 to this, all the bits are set to 1, thus giving you the maximum value of unsigned long long.
•  » » » » » » » 7 weeks ago, # ^ |   0 Thanks for this elaborate answer.
 » 7 weeks ago, # |   +1 or you can use for (size_t i = ...) { ... } but this is better for (int i = 0; i < int(vec.size()); ++i) { ... } 
 » 7 weeks ago, # |   +10 C++20 contains the method ssize which returns an int value for the vector size. See https://en.cppreference.com/w/cpp/iterator/size for details. I hope to see C++20 soon here on Codeforces :).
 » 4 weeks ago, # | ← Rev. 2 →   0 What is the maximum size of long long type vector? I tried to understand from this link: https://stackoverflow.com/questions/3813124/c-vector-max-size#:~:text=max_size()%20is%20the%20theoretical,or%202%5E29%20double%20values. But I couldn't understand it clearly. Because it says cout<