C plus plus Arrays

Like other programming languages, Array in C plus plus is a group of similar types of elements that have contiguous memory location.

In C++ std::array is a container that encapsulates fixed size arrays. In C++, array index starts from 0. We can store only fixed set of elements in C++ array.

C plus plus Array


Advantages of Array

  • 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 2 types of arrays in C++ programming:

  1. Single Dimensional Array
  2. Multidimensional Array

C++ Single Dimensional Array

Let’s see a simple example of C++ array, where we are going to create, initialize and traverse array.

 
  • #include <iostream>  
  • using namespace std;  
  • int main()  
  • {  
  •  int arr[5]={10, 0, 20, 0, 30};  //creating and initializing array    
  •         //traversing array    
  •         for (int i = 0; i < 5; i++)    
  •         {    
  •             cout<<arr[i]<<“\n”;    
  •         }    
  • }  

Output:

10
0
20
0
30

C++ Array Example: Traversal using for each loop

We can also traverse the array elements using for each loop. It returns array element one by one.

 
  • #include <iostream>  
  • using namespace std;  
  • int main()  
  • {  
  •  int arr[5]={10, 0, 20, 0, 30}; //creating and initializing array    
  •         //traversing array    
  •        for (int i: arr)     
  •         {    
  •             cout<<i<<“\n”;    
  •         }    
  • }  

Output:

10
20
30
40
50

Leave a Reply