1

For c++ STL we can insert new value by using "push_back", is there a push_back times limit if we never pop_back?

In other words, what is the maximum size of vector?

Because when declare vector, we don't need to specify the size of vector, so I am not sure the maximum size of vector.

Thanks.

I just print out v.max_size(), it is 9223372036854775807 in xcode, I think this is the size limit.

hellocoding
  • 221
  • 4
  • 13

1 Answers1

4

std::vector::max_size() returns the maximum number of elements that the vector can hold. However, it is not guaranteed that the vector will be able to grow to that size. To quote from http://www.cplusplus.com/reference/vector/vector/max_size/ :

This is the maximum potential size the container can reach due to known system or library implementation limitations, but the container is by no means guaranteed to be able to reach that size: it can still fail to allocate storage at any point before that size is reached

digital_revenant
  • 3,274
  • 1
  • 15
  • 24
  • 2
    +1 [Same member, different site source](http://en.cppreference.com/w/cpp/container/vector/max_size). – WhozCraig Oct 19 '13 at 18:42
  • I just print out v.max_size(), it is 9223372036854775807 in xcode, I think this is the size limit.(at least potential size) – hellocoding Oct 19 '13 at 18:57
  • About the size limit, 1) does it depend on the pc, like different PCs have different memories 2) Or it depends on the rules of C++ STL standard. – hellocoding Oct 19 '13 at 18:58
  • @hellocoding Yes, it depends on the amount of available memory on your system among various other factors. Also `Allocator` class defines the memory models to be used by STL containers. So, max size would depend on its implementation too. – digital_revenant Oct 19 '13 at 20:40