C++ Arrays
In C++, an array is a collection of elements, all of the same type, stored in contiguous memory locations. Arrays are used to store multiple values in a single variable, rather than declaring individual variables for each value. Arrays are particularly useful when working with large amounts of data that share the same data type.
In this blog post, we will:
An array is a collection of elements of the same type, stored in contiguous memory locations. The size of an array is defined at the time of its creation, and once defined, the size cannot be changed.
Arrays in C++ are indexed, meaning each element is accessed using its index, starting from 0
for the first element.
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {1, 2, 3, 4, 5}; // Array of integers
cout << "First element: " << numbers[0] << endl; // Accessing the first element
cout << "Second element: " << numbers[1] << endl; // Accessing the second element
return 0;
}
Output:
First element: 1
Second element: 2
In this example:
numbers
is defined to hold 5 integers.numbers[0]
, numbers[1]
, etc.The syntax to declare an array in C++ is as follows:
data_type array_name[array_size];
int
, float
, char
).
int ages[3]; // Array of 3 integers
You can also initialize an array at the time of declaration:
int ages[3] = {25, 30, 35}; // Array of 3 integers initialized with values
If the size is not specified during initialization, the array will automatically size itself based on the number of elements:
int ages[] = {25, 30, 35}; // Array of 3 integers
C++ supports the following types of arrays:
A one-dimensional array is the most basic type, where all elements are arranged in a single row.
#include <iostream>
using namespace std;
int main() {
int numbers[4] = {1, 2, 3, 4}; // One-dimensional array
for (int i = 0; i < 4; i++) {
cout << "Element at index " << i << ": " << numbers[i] << endl;
}
return 0;
}
Output:
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
A two-dimensional array can be thought of as an array of arrays. It is useful for representing matrices or tables.
#include <iostream>
using namespace std;
int main() {
int matrix[2][2] = {{1, 2}, {3, 4}}; // Two-dimensional array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
cout << "Element at [" << i << "][" << j << "]: " << matrix[i][j] << endl;
}
}
return 0;
}
Output:
Element at [0][0]: 1
Element at [0][1]: 2
Element at [1][0]: 3
Element at [1][1]: 4
C++ supports arrays with more than two dimensions, though these are less commonly used. Multidimensional arrays are often used in scientific computing, simulations, and graphics.
#include <iostream>
using namespace std;
int main() {
int arr[2][2][2] = {
{{1, 2}, {3, 4}},
{{5, 6}, {7, 8}}
}; // Three-dimensional array
cout << "Element at [1][1][1]: " << arr[1][1][1] << endl;
return 0;
}
Output:
Element at [1][1][1]: 8
Elements in an array are accessed using indexes, starting from 0
. The index specifies the position of the element within the array.
Here’s how you can access and modify array elements:
#include <iostream>
using namespace std;
int main() {
int numbers[3] = {10, 20, 30};
cout << "First number: " << numbers[0] << endl; // Accessing the first element
cout << "Second number: " << numbers[1] << endl; // Accessing the second element
return 0;
}
Modifying Array Elements:
#include <iostream>
using namespace std;
int main() {
int numbers[3] = {10, 20, 30};
numbers[0] = 100; // Modifying the first element
cout << "Updated first number: " << numbers[0] << endl;
return 0;
}
Output:
Updated first number: 100
You can calculate the size of an array using the sizeof
operator in C++. It can help determine the number of elements in an array:
#include <iostream>
using namespace std;
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
int size = sizeof(numbers) / sizeof(numbers[0]); // Size of the array
cout << "Size of the array: " << size << endl;
return 0;
}
Output:
Size of the array: 5
Here:
sizeof(numbers)
gives the total size of the array in bytes.sizeof(numbers[0])
gives the size of one element (in this case, an int
).While arrays are widely used in C++, they have some limitations:
std::vector
.