3 ms·
But... Sometimes you do. Can you imagine how stupid it would feel if you had to abandon all your std::vector-using code the moment you need to deal with an arr
by blt 11y ago
But... Sometimes you do.
Can you imagine how stupid it would feel if you had to abandon all your std::vector-using code the moment you need to deal with an array bigger than 2 billion elements?
- imron 11y agoActually, thinking about it a bit more, the only thing you could store in a std::vector indexed by signed ints where you would have a problem would be single bytes. If you have 2 billion 2 byte elements then that's 4gb which is the total addressable space on a 32-bit system - leaving no room for program code or any other data meaning you couldn't run anything. You could go 64bit, but then signed ints go up to (2^63)-1 and you have the same problem - elements more than a single byte in size will cause you to run out of addressable memory before you exhaust available signed ints.
- okasaki 11y agoVector<bool> is often specialized to take 1 bit per element, so that's another one.
- imron 11y agoYes, and like a vector containing > 2 billion single byte elements it's still delving in to a very peculiar use case. Compared to the majority of other uses of std::vector which will never be able to overflow signed ints. I get from a logical point of view that size() would never be < 0 therefore it makes sense to use an unsigned int, however from a practical point of view it also makes programs more susceptible to a number of pernicious bugs that can catch out unsuspecting programmers, e.g. things like for ( size_t i = v.size(); i >= 0; --i ) { ... } Which would work fine with a signed int, but here it will just loop infinitely.