Vector Methods

Vector Methods

Vectors have a handful of useful functions, which you can see here. In this part of the lesson, you will go over the ones you will be using in the object oriented programming lesson.

assign

Assign helps you quickly populate a vector with fixed values. For example this code,

vector<int> intvariable;
intvariable.assign(10,16);

is going to populate the vector with ten integers all having the value of 16.

The assign method lets you override your current vector with a new vector.

Remember, you've already seen a similar way to initialize values in a vector:

vector<int> intvariable(10,16);

The difference is that the assign method lets you override your current vector with new values.

push_back

Pushback adds an element to the end of the vector:

vector<int> intvariable;
intvariable.push_back(25);

size

Size returns the size of the vector.

intvariable.size();

Last updated