Vector resize() in C++

Vectors are called dynamic arrays and can automatically adjust their size when a component is added or deleted. This container is used for storage.

The function modifies the real content of the container by inserting or deleting the components from it. Thus it occurs,

  1. If at present the given value of n is less than the size, then additional elements are demolished.
  2. If n is greater than the current container size, then the next components are modified at the end of the vector.

Syntax:

Parameters:

N – The container size is new, displayed in the number of objects.

Val -If this variable is specified, new features with this value are initialized.

Return value:

This method contains nothing.

Exception:

The only exception if this occurs is thrown by Bad alloc, if it keeps failing to reallocate.

The programs below show how the function works

The size of the vector container is lowered.

Output:

Vector resize() in C++

Size of the vector container is increased.

Output:

Vector resize() in C++

The size of the vector container is increased, and new elements are initialized with the specified value.

Output:

Vector resize() in C++

Pin It on Pinterest

Share This