Array in c++
In C++, an array is a collection of elements of the same data type stored in contiguous memory locations. Each element in the array is accessed by its index or position. The index starts from 0 and goes up to (size - 1), where size is the number of elements in the array.
Here's a basic example of how to declare, initialize, and use an array in C++:
#include <iostream>
int main() {
// Declaration and initialization of an integer array with size 5
int myArray[5] = {10, 20, 30, 40, 50};
// Accessing and printing elements of the array
std::cout << "Elements of the array:" << std::endl;
for (int i = 0; i < 5; ++i) {
std::cout << "Element at index " << i << ": " << myArray[i] << std::endl;
}
return 0;
}
In this example, myArray is an integer array with five elements. The elements are initialized using an initializer list. The for loop is then used to iterate over the array and print each element along with its index.
You can also use standard library containers like std::vector for more dynamic arrays with dynamic sizes and additional functionality. However, basic arrays as shown in the example above are a fundamental part of the C++ language.