🎓 A1Learner
🔍
0 of 34 lessons completed

C++ Vectors in Depth

Vector capacity is the amount of storage a vector has ready for its elements. This lesson separates size from capacity and shows insert and erase at a position.

Capacity grows in steps

Each push adds one element, and the print after it shows the size and the capacity. The capacity stays flat for some pushes and jumps at others.

#include <iostream>
#include <vector>

int main() {
  std::vector<int> numbers;
  numbers.push_back(10);
  std::cout << numbers.size() << "/" << numbers.capacity() << "\n";
  numbers.push_back(20);
  std::cout << numbers.size() << "/" << numbers.capacity() << "\n";
  numbers.push_back(30);
  std::cout << numbers.size() << "/" << numbers.capacity() << "\n";
  numbers.push_back(40);
  std::cout << numbers.size() << "/" << numbers.capacity() << "\n";
  numbers.push_back(50);
  std::cout << numbers.size() << "/" << numbers.capacity() << "\n";
  numbers.push_back(60);
  std::cout << numbers.size() << "/" << numbers.capacity() << "\n";
  return 0;
}

reserve sets the capacity early

reserve asks for room for ten elements while the vector is still empty. Three pushes then fit inside that room, so the capacity stays at 10.

#include <iostream>
#include <vector>

int main() {
  std::vector<int> numbers;
  numbers.reserve(10);
  std::cout << "Size: " << numbers.size() << " Capacity: " << numbers.capacity() << "\n";
  numbers.push_back(10);
  numbers.push_back(20);
  numbers.push_back(30);
  std::cout << "Size: " << numbers.size() << " Capacity: " << numbers.capacity() << "\n";
  return 0;
}

shrink_to_fit and clear

Three pushes leave a capacity of 4 for a size of 3. shrink_to_fit drops the spare room, and clear then empties the vector while the capacity stays.

#include <iostream>
#include <vector>

int main() {
  std::vector<int> numbers;
  numbers.push_back(10);
  numbers.push_back(20);
  numbers.push_back(30);
  std::cout << "Size: " << numbers.size() << " Capacity: " << numbers.capacity() << "\n";
  numbers.shrink_to_fit();
  std::cout << "Size: " << numbers.size() << " Capacity: " << numbers.capacity() << "\n";
  numbers.clear();
  std::cout << "Size: " << numbers.size() << " Capacity: " << numbers.capacity() << "\n";
  return 0;
}

insert and erase at a position

A position from begin plus an offset marks a place inside the vector. insert puts a value at that place, and erase removes the value at a place.

#include <iostream>
#include <vector>

int main() {
  std::vector<int> numbers = {10, 20, 30};
  auto first = numbers.begin() + 1;
  numbers.insert(first, 15);
  std::cout << "After insert:" << "\n";
  for (int number : numbers) {
    std::cout << number << "\n";
  }
  auto second = numbers.begin() + 2;
  numbers.erase(second);
  std::cout << "After erase:" << "\n";
  for (int number : numbers) {
    std::cout << number << "\n";
  }
  return 0;
}

Example

#include <iostream>
#include <string>
#include <vector>

int main() {
  std::vector<std::string> waiting;
  waiting.reserve(5);
  waiting.push_back("Ana");
  waiting.push_back("Theo");
  auto front = waiting.begin();
  waiting.insert(front, "Mina");
  auto last = waiting.begin() + 2;
  waiting.erase(last);
  for (std::string name : waiting) {
    std::cout << name << "\n";
  }
  std::cout << "Capacity: " << waiting.capacity() << "\n";
  return 0;
}

Key points

  • size gives the number of elements a vector holds.
  • capacity gives the number of elements a vector can hold before it needs larger storage.
  • The library chooses when to grow the capacity as elements are added.
  • reserve asks for a capacity at once and does not change the size.
  • shrink_to_fit asks a vector to give back storage it does not use.
  • clear removes all elements and leaves the capacity unchanged.
  • insert and erase take a position and shift the elements after it.

In this example, the waiting list reserves room for five names before any name arrives. Mina is inserted at the front position, and the name at the last position is erased. The list prints Mina and Ana, and the capacity stays at the reserved 5.

Note

The Iterators lesson later in this course gives positions their full treatment.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

What does reserve(10) do to a new empty vector?