Language: EN

cpp-que-son-arrays

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

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.

Array declaration

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

Type arrayName[size];

Where:

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

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

int myNumbers[5];

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

Creating and initializing arrays

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

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

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

In this example, the array numbers 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 (generally 0 for numeric types):

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

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

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]

For example:

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

Modifying elements

You can modify the elements of the array 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

Common operations with arrays

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

Sorting an array

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

#include <algorithm>

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

Reversing an array

To reverse the order of the 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

Multidimensional arrays

Arrays can have more than one dimension, allowing for the storage of data in more complex structures like matrices.

Example of a two-dimensional array

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

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

Accessing elements in multidimensional arrays

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

Practical examples

:::::::