4

I have a piece of code which creates a std::vector<T> with a known size:

std::vector<T> vectorOfTs(n);

Does calling push_back increase the size to n+1?

vectorOfTs.push_back(T());
tunnuz
  • 23,338
  • 31
  • 90
  • 128
  • 1
    You included memory allocation as a tag; could you elaborate on what you expect and what's confusing you in that aspect? –  Nov 11 '10 at 10:17

4 Answers4

19

Yes; note that vector<T>.capacity() is different from vector<T>.size(). The latter denotes the number of elements currently in the vector while the former represents the number of items that fit in the space currently allocated for the vector's internal buffer.

Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
10

Almost. If there are no exceptions, then size() will increment.

push_back(T()) could also throw an exception at various stages: see here, or summarily:

  • T() construction, in which case no call to push_back takes place, and size() is unaffected

  • if the vector needs to increase the capacity, that may throw, in which case size() is unaffected

  • the vector element will be copy or move constructed using std::allocator_traits<A>::construct(m, p, v);, if A is std::allocator<T>, then this will call placement-new, as by ::new((void*)p) T(v): if any of this throws the vector's size() is unaffected, ****unless***

    • the move constructor isn't noexcept and does throw: in which case the effects are unspecified
  • the vector update's then complete - size() will have incremented and the value will be in the vector (even if T::~T())

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
4

Yes. If you instead want to reserve space, call reserve(), e.g.:

std::vector<T> vectorOfTs;
vectorOfTs.reserve(n);
// now size() == 0, capacity() >= n

vectorOfTs.push_back(T());
// now size() == 1
Nim
  • 33,299
  • 2
  • 62
  • 101
  • 1
    Your answer seems a bit ambiguous, at best. `reserve` will *not* change the size, it just reserves memory (and therefore may change `capacity`). – GManNickG Nov 11 '10 at 10:01
  • Hmm really? I was presenting this as a different option, if the OP's intention is to pre-allocate memory for a vector of size n rather than have a vector of n default constructed Ts, he could use `reserve()`, but the main answer is "Yes". – Nim Nov 11 '10 at 10:12
  • 1
    You were 99% of the way there; I tried to edit and remove the ambiguity GMan mentioned. (The original question is vague on what is expected and what's confusing about the allocation aspect.) –  Nov 11 '10 at 10:14
0

Yes.

std::vector<T> vectorOfTs(n);

In the above statement, actually you are constructing 'n' number of new instances of type T (i.e. default constructor T() would be triggered for each time). Now vector vectorOfTs contains n elements. The following version of the vector constructor would be invoked for the above statement.

explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() );

So, when you push back another element into vector, size of vector would be n+1.

jRJ
  • 268
  • 2
  • 5