cpp-que-son-arrays

What are arrays and how to use them in C++

  • 5 min

In C++ an Array is a collection of elements of the same type, of fixed size stored in contiguous memory locations.

Each element of the array can be accessed using a numerical index, starting from zero. For this, the [] operator is used.

If you want to learn more, check out the Introduction to Programming Course

Array Declaration

The basic syntax for declaring an array in C++ is:

Type arrayName[size];
Copied!

Where:

  • Type: Is the data type the array will contain, such as int, double, char, etc.
  • arrayName: Is the identifier of the array.
  • size: Is the number of elements the array can hold.

For example, to declare an array of integers with 5 elements:

int myNumbers[5];
Copied!

In this case, myNumbers is an array that can hold 5 integers.

Array Creation and Initialization

Once an array is declared, it can be initialized in several ways:

You can initialize an array with specific values at the moment of declaration:

int numbers[5] = {1, 2, 3, 4, 5};
Copied!

In this example, the numbers array is initialized with the values 1, 2, 3, 4, and 5.

If you initialize an array with fewer elements than the declared size, the remaining elements will be initialized with default values (usually 0 for numeric types):

int numbers[5] = {1, 2}; // The array is initialized as {1, 2, 0, 0, 0}
Copied!

C++ also allows automatic size initialization of the array if a list of values is provided:

int numbers[] = {1, 2, 3, 4, 5}; // The size of the array is automatically adjusted to 5
Copied!

Using the Array

Accessing Elements

Each element of an array is accessed using an index, which starts at 0. The syntax to access an element is:

arrayName[index]
Copied!

For example:

int numbers[] = {1, 2, 3, 4, 5};
int firstNumber = numbers[0]; // firstNumber will be 1
Copied!

Modifying Elements

You can modify array elements by assigning new values to specific indices:

int numbers[] = {1, 2, 3, 4, 5};
numbers[2] = 10; // The third element of the array will now be 10
Copied!

Common Array Operations

Array Length

To get the number of elements in an array, you can use the sizeof operator:

int numbers[] = {1, 2, 3, 4, 5};
int length = sizeof(numbers) / sizeof(numbers[0]); // length will be 5
Copied!

Sorting an Array

To sort arrays you can use the sort function from the standard <algorithm> library:

#include <algorithm>

int numbers[] = {5, 3, 1, 4, 2};
std::sort(numbers, numbers + 5); // Sorts the array in ascending order
Copied!

Reversing an Array

To reverse the order of elements, you can also use the reverse function from <algorithm>:

#include <algorithm>

int numbers[] = {1, 2, 3, 4, 5};
std::reverse(numbers, numbers + 5); // Reverses the array
Copied!

Multidimensional Arrays

Arrays can have more than one dimension, allowing data to be stored in more complex structures like matrices.

int matrix[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};
Copied!

In this example, matrix is a two-dimensional array with 2 rows and 3 columns.

To access elements in multidimensional arrays you would do it like this,

std::cout << "Element in row 1, column 2: " << matrix[1][2] << std::endl; // Prints 6
Copied!

Practical Examples