Start learning free

LESSON 108 OF 667 · ARRAYS I

Appending Item to Vector in C++

Lesson

We can add elements to the end of a vector using the .push_back() method.

.push_back() takes one parameter and "pushes" it onto the end of the vector:

vector<int> arr1 = {1, 2, 3};
arr1.push_back(4);

// arr1 is now {1, 2, 3, 4}

Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check