C++ · ОСНОВЫ · LESSON 15

Add to the list

push_back · about 12 minutes

Learning with progressRead the summary for free. To save the work, log in to the account.
STEP 1

How it works.

push_back puts a new element at the end of vector.

nums.push_back(3);

✦ Once added, size() will become larger.

STEP 2

Try to understand.

Once added, size() will become larger.

vector<int> nums = {1, 2};
nums.push_back(3);
cout << nums.size() << endl;

✦ Compare this to the first one. Then do the cool job yourself.

IN-CLASS PRACTICE

Type the code yourself

Manually type the program from the sample. So you'll remember commands and brackets.

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

int main() {
    vector<int> nums = {1, 2};
    nums.push_back(3);
    cout << nums.size() << endl;
    return 0;
}
Open the editor for class work
HOMEWORK

Solve it yourself

Создай vector<int> nums из 1 и 2. Добавь 3 и 4 через push_back, затем выведи размер.

The answer is hidden. Use similar examples from the lesson notes.

Opening up your homework