Menu
Coddy logo textTech

Adding Elements

Part of the Logic & Flow section of Coddy's C++ journey — lesson 11 of 56.

Now that you know how to create vectors, let's learn how to add elements to them after they've been created. The push_back() method is your primary tool for growing a vector by adding new elements to its end.

The push_back() method takes a single argument - the value you want to add - and appends it to the back of the vector:

std::vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);

In this example, we start with an empty vector and use push_back() to add three integers. After these operations, our vector contains the elements [10, 20, 30] in that order.

The beauty of push_back() is that it handles all the memory management automatically. When you add elements and the vector runs out of space, it automatically allocates more memory and copies the existing elements to the new location. This makes vectors incredibly convenient for building collections of data when you don't know the final size in advance.

challenge icon

Challenge

Easy

Create a program that demonstrates adding elements to a vector using the push_back() method. You'll start with an empty vector and dynamically build it by adding numbers in a specific sequence.

The following inputs will be provided:

  • An integer n representing how many numbers to add to the vector
  • Then n integers that should be added to the vector one by one

Your program should:

  1. Create an empty std::vector<int> named numbers
  2. Use a loop to read each of the n input numbers
  3. For each number, use push_back() to add it to the end of the vector
  4. After adding each number, print the current size of the vector and the number that was just added
  5. Finally, print all elements in the vector on a single line, separated by spaces

Use the following exact output format:

Added [number], size is now [current size]
Added [number], size is now [current size]
...
Final vector: [num1] [num2] [num3] ...

Use the .size() method to get the current number of elements in the vector after each addition. For the final output, use the square bracket [] operator to access each element by index.

Cheat sheet

Use push_back() to add elements to the end of a vector:

std::vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);

The push_back() method automatically handles memory management, expanding the vector as needed when adding elements.

Try it yourself

#include <iostream>
#include <vector>
using namespace std;

int main() {
    // Read the number of elements to add
    int n;
    cin >> n;
    
    // Create an empty vector
    vector<int> numbers;
    
    // TODO: Write your code here
    // Read n numbers and add them to the vector using push_back()
    // Print the required output after each addition
    
    // Print final vector
    cout << "Final vector: ";
    // TODO: Print all elements separated by spaces
    
    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow