What is the usage limit of . push_back()?

Asked

Viewed 59 times

3

I use a lot .push_back(element) when I’m working with vector, but I don’t know what’s the most elements I can add to a vector before it overflows and performs the reallocation of a new space in memory.

Is there any mechanism I can use to know as many elements as the vector supports? This value is always the same, so if you compile for any machine this value will not change?

2 answers

2


  • 1

    https://gcc.gnu.org/onlinedocs/gcc-4.6.3/libstdc++/api/a01115_source.html

  • 1

    Most recent: https://gcc.gnu.org/onlinedocs/gcc-7.2.0/libstdc++/api/a15915_source.html

2

Is there any mechanism I can use to know as many elements as the vector supports?

Yes,

  • std::vector::capacity returns the number of elements to which the container has currently allocated space, and
  • std::vector::max_size returns the maximum number of elements that the container is able to store due to system limitations or library implementation.

This value is always the same, so if you compile for any machine this value will not change?

No, these values may change according to the system and implementation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.