How to insert an element in an array in C++

In this tutorial, we are going to discuss How to insert elements in an array? Firstly there is an introduction to arrays then different ways to declare an array. And finally, we will discuss the insertion of elements.

Insert an element in an Array in C++

Array is a data structure which is a collection of primitive data types. For example array of integers, the array of characters and etc. It comes in use when we want to save large data of the same type.

Syntax:

#include<bits/stdc++.h>
int main(){
    int ar[10];
    char str[10];
    float far[10];
    
    return 0;
}

In the above example, there are three types of arrays define. First, one “ar” is of integer type it can save 10 integer values. Then the next one “str” is an array of character type it can save data like names, sentences and etc. Array with name “far” is an array of float type it can save data of integers with decimals.

Types of Declaration

Declaration of the array means to creation and allocation of memory to it.



There are two types of arrays: Static or Dynamic

  • Static: In the static declaration the size of the array has to predefine.
    Example:

    int ar[10];

    Above code shows that the number of elements is predefined. This array can save 10 elements of integer type.

  • Dynamic: In a dynamic declaration, the size of the array does not have to be predefined.
    Example:

    int size;
    cin >> size;
    int *array = new int[size];
    delete [] array;

    In this code, the size variable defines the size of the array. This variable can be given any value while runtime. Therefore it is a dynamic array.

Insert elements in an array in C++

Once the array is defined then it is time to insert elements into it. Elements inside array can be added at runtime as well as at the time of declaration.

At the time of declaration:

To insert value inside the array at the time of declaration is called initialization with the declaration.
Demonstration:

#include<iostream>
using namespace std;

int main(){
  int ar[]={1,2,3,4,5};
  for(int i=0;i<5;i++)
  cout<<ar[i]<<"\n";
  return 0;
}

Output:

1
2
3
4
5

Array “ar” is declared and initialized at same time. This array has 5 elements valued as {1,2,3,4,5}

At runtime:

 To insert value inside the array at runtime.

Demonstration:

First method:

#include<iostream>
using namespace std;
int main(){
int arr[5];
for(int i=0;i<5;i++)
cin>>arr[i];
for(int i=0;i<5;i++)
cout<<arr[i]<<"\n";
return 0;
}

Input:

1 2 3 4 5

Output:

1
2
3
4
5

In this method, a static array is created and data is added in it.

Second Method:

#include <iostream>
#include <vector>

using namespace std;

int main ()
{
  vector<int> myvector;
  for (int i=1; i<=5; i++)
   myvector.push_back(i);

  for (vector<int>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
  cout << *it<<"\n";

  return 0;
}

Output:

1
2
3
4
5

Vector is a data structure defined in c++ STL. It acts as a dynamic array.

Also, See:

Leave a Reply

Your email address will not be published. Required fields are marked *