I have two vector a, b. I wanna compare the size of them. I know I can use if (a.size() > b.size()). But my question is if the size are too big out of the type int for a or/and b. For example, a.size() is 99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999, etc. How can I compare the sizes of a and b? sorry for my English.
1 Answer
There's no way the size of a vector (or any object) can be greater than representable by std::size_t. That type can, by definition, store the size of the largest object a program can create.
As std::size_t is the type returned by a std::vector's (or any other standard library container's) size() function, you're safe in comparing them.
Reply to question in comments:
std::size_t is not unlimited (in a finite computer, nothing is). But it's guaranteed to be large enough to hold the size of any object possible. Note ion particular than an array is also an object. std::size_t is also not limited by RAM exactly, more by the platform you're building for. Examples:
On a normal 32-bit system (e.g. Win32), the address space is 32-bit and the largest object can (in theory) occupy 232 bytes, so
std::size_tis at least 32 bits long.On a normal 64-bit system (e.g. Win64), the address space is 64-bit and the largest object can (in theory) occupy 264 bytes, so
std::size_tis at least 64 bits long.In x86 real mode, the address space is 20-bit, but no object can occupy more than one segment. As segments are 216 bytes, 16 bits are enough for
std::size_twhen the target platform is x86 real mode.
vector's size, while technically unbounded, is practically limited to the amount of available RAM.10^134 - 1(ignoring the 'etc.'). Given that there are an estimated10^80atoms in the observable universe, it seems unlikely you'll ever find enough storage for such a vector.