Arrays in C++ language
An array is a set of similar types of elements that have contiguous memory location.
A std::array in C++ is a container that holds fixed-sized arrays. In C++, array index starts at 0. We can store only fixed set of elements in C++ array.
An array of int or float datatypes are arrays but arrays of char datatypes are called strings.
Declaration of array
Similar to the variable, arrays are declared before they are being used for the benefit of the compiler. The general form of the declaration an array is,
Array initialization
Advantages of C++ Array
The most common advantage of C++ is that,
- Code Optimization (less code)
- Random Access
- Easy to traverse data
- Easy to manipulate data
- Easy to sort data etc.
Disadvantages of C++ Array
- Fixed size
C++ Array Types
There are 3 types of arrays in C++ programming:- Single Dimensional Array
- Two Dimensional Array
- Multidimensional Array
Let's look at a simple example of a C++ array,
Output:
10
20
30
40
50