0

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.

3
  • A vector's size, while technically unbounded, is practically limited to the amount of available RAM. Commented May 17, 2013 at 14:49
  • the size of a vector isn't stored in an int it's stored in a size_t, which i s a special int type. A vector can only store max value of size_t elements, if you get to this maximum you have more issues than comparing vector sizes, it's likely only bounded by address space, not the limitations of they size_t type, but I've never bothered to look into it. Commented May 17, 2013 at 14:50
  • 1
    the value you list is 10^134 - 1 (ignoring the 'etc.'). Given that there are an estimated 10^80 atoms in the observable universe, it seems unlikely you'll ever find enough storage for such a vector. Commented May 17, 2013 at 15:26

1 Answer 1

8

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_t is 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_t is 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_t when the target platform is x86 real mode.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you mean size_t is unlimited, only RAM can limit the size of vector?
@kaji331 I've added a reply to the answer.
Thanks. You mean object and size_t is limited by platform, so I can't create a unlimited vector even if I have "unlimited" RAM, then size_t is enough to be compared?
@kaji331 Yes, that's pretty much it. Note that on today's non-embedded platforms, the platform limitation will most likely be the size of the address space, which is an upper limit on the amount of 'unlimited' RAM you can have.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.