C++ Arrays
arrays in C++ are a fundamental concept used to store multiple values of the same type in a single variable. Here’s a comprehensive overview:
IMAGE SOURCE :- GFG
Declaration and Initialization
- Declaration: To declare an array in C++, you specify the type of its elements and the number of elements (size):
int numbers[5]; // Declares an array of 5 integers
- Initialization: You can initialize an array at the time of declaration:
int numbers[5] = {1, 2, 3, 4, 5}; // Initializes all elements
If you don’t initialize all elements, the remaining elements will be set to zero (for fundamental types):
int numbers[5] = {1, 2}; // numbers[0] = 1, numbers[1] = 2, numbers[2] to numbers[4] = 0
You can also leave the size unspecified when you provide the initialization:
int numbers[] = {1, 2, 3, 4, 5}; // Compiler determines the size (5 in this case)
Accessing Elements
You can access array elements using the index, which starts from 0:
int first = numbers[0];
// Access the first element
numbers[1] = 10; // Modify the second element
Iterating Through Arrays
You can use loops to iterate through array elements:
// Using a for loop
for(int i = 0; i < 5; ++i) {
std::cout << numbers[i] << " ";
}
Multidimensional Arrays
C++ supports multidimensional arrays. Here’s how you declare and initialize a 2D array:
// Declaration
int matrix[3][4];
// Initialization
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Accessing elements
int element = matrix[1][2]; // Access the element at second row, third column (value 7)
Common Operations
- Finding the Size:
To find the number of elements in a statically declared array:
int size = sizeof(numbers) / sizeof(numbers[0]);
- Copying Arrays:
Arrays cannot be directly copied using the assignment operator. You need to use a loop or
std::copy
from the<algorithm>
header:int source[5] = {1, 2, 3, 4, 5};
int destination[5];
// Using a loop
for(int i = 0; i < 5; ++i) {
destination[i] = source[i];
}
// Using std::copy
std::copy(std::begin(source), std::end(source), std::begin(destination));
- Passing Arrays to Functions:
Arrays are passed to functions by reference (the pointer to the first element is passed). Here’s how you define and call a function that takes an array:
void printArray(int arr[], int size) {
for(int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
}
int main() {
int numbers[] = {1, 2, 3, 4, 5};
printArray(numbers, 5);
return 0;
}
Dynamic Arrays
For dynamically-sized arrays, C++ provides the std::vector
from the Standard Library, which offers a more flexible and safer alternative to C-style arrays. Here’s a quick example:
std::vector<int>
numbers = {1, 2, 3, 4, 5};numbers.push_back(6); // Add an element to the end
int size = numbers.size(); // Get the number of elements
Summary
- Arrays in C++ are a collection of elements of the same type stored in contiguous memory locations.
- You can declare, initialize, and access elements of arrays using indices.
- Use loops to iterate over arrays.
- C++ supports multidimensional arrays.
- For dynamic arrays, consider using
std::vector
for better safety and functionality.
0 Comments