
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to add an element in the array at the beginning
The storage of homogenous (same) data across several memory locations is made possible by the use of arrays and data structures. The key benefit of using arrays is that we can use the index argument to retrieve them from anywhere we want. This data structure becomes linear because data must be inserted and withdrawn progressively. All we need to do is place the index or position number for that element inside the square bracket to retrieve it from the array. In this article, we will take array A and another element e. We will insert e into A at the starting position in C++.
Understanding the concept with examples
Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69] After inserting 23 at the end, the array will look like this: [23, 10, 14, 65, 85, 96, 12, 35, 74, 69]
Here in the above example, we have an array A with nine elements in it. We are going to insert another element 23, at the beginning of array A. The resultant array is holding all of the elements along with 23 at the beginning. To insert an element at the beginning, we must shift all elements towards the right for one place, then the first slot will be empty and we put the new element in that position. Let us see the algorithm for a clear understanding.
Algorithm
Take an array A and an element e
if the array A has sufficient space to insert element e, then
for i from range n - 1 to 0, do
A[ i + 1 ] = A[ i ]
end for
A[ 0 ] = e
increase n by 1
end if
return A
Example
#include <iostream> # define Z 50 using namespace std; void displayArr(int arr[], int n){ for( int i = 0; i < n; i++ ){ cout << arr[ i ] << ", "; } cout << endl; } void insertAtBeginning( int A[], int &n, int e ){ if( n + 1 < Z ) { for( int i = n - 1; i >= 0; i-- ) { A[ i + 1 ] = A[ i ]; } A[ 0 ] = e; n = n + 1; } } int main() { int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; int n = 12; cout << "Array before insertion: "; displayArr( A, n ); cout << "Inserting 58 at the beginning:" << endl; insertAtBeginning( A, n, 58 ); cout << "Array after insertion: "; displayArr( A, n ); cout << "Inserting 225 at the beginning:" << endl; insertAtBeginning( A, n, 225 ); cout << "Array after insertion: "; displayArr( A, n ); }
Output
Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Inserting 58 at the beginning: Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Inserting 225 at the beginning: Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
Using Vectors insert an element at the front
The vectors are a dynamic data structure that comes with C++ STL. We can get a similar functionality like arrays in vectors as well. But inside vectors, we can just insert at the end or back. There is no direct method to insert at the beginning. However, we can either follow a similar way like before shifting elements to one place, then insert the new element at the beginning. Or we can create another single−element vector with the new one and then concatenate them. So the resultant vector will hold all previous elements with the new one at the beginning. Let us see the algorithm and C++ implementation.
Algorithm
Take an array A and an element e
Create an empty vector B
insert e into B
A := Concatenate B with A (first B then A)
return A
Example
#include <iostream> #include <vector> # define Z 50 using namespace std; void displayArr( vector<int> v ){ for( int i = 0; i < v.size() ; i++ ){ cout << v[ i ] << ", "; } cout << endl; } vector<int> insertAtBeginning( vector<int> A, int e ){ vector<int> B( 1 ); B[ 0 ] = e; B.insert( B.end(), A.begin(), A.end() ); return B; } int main() { vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14}; cout << "Array before insertion: "; displayArr( A ); cout << "Inserting 58 at the beginning:" << endl; A = insertAtBeginning( A, 58 ); cout << "Array after insertion: "; displayArr( A ); cout << "Inserting 225 at the beginning:" << endl; A = insertAtBeginning( A, 225 ); cout << "Array after insertion: "; displayArr( A ); }
Output
Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Inserting 58 at the beginning: Array after insertion: 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, Inserting 225 at the beginning: Array after insertion: 225, 58, 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14,
Conclusion
In this article, we have seen how to insert elements at the beginning of an array. There are two different solutions that we have discussed here. In the first solution that static C++ array is used and in the second solution vectors are used. The vectors have no direct method to insert an element at the beginning. We can insert at the end directly using the push_back() method. For that, we used a trick, where we created an array of size 1 and then insert the new element into it. After that concatenate the with the given array. We can use lists to perform the same effect. However C++ lists have the push_front() method which can directly insert an element at the front. But lists are not arrays, they are linked lists.
- Related Articles
- Golang Program to add an element in the array at the beginning
- PHP program to add item at the beginning of associative array
- How to push an element at the beginning of an array in TypeScript?
- How to add new array elements at the beginning of an array in JavaScript?
- Add an element to an array in Swift
- Golang Program to insert an element into the array at the specified index
- Python Program to insert an element into the array at the specified index
- C# program to find the last matching element in an array
- C# Program to display the first element from an array
- C# program to get the last element from an array
- C# Program to find the smallest element from an array
- C# Program to find the largest element from an array
- C++ Program to append an element into an array
- Program to get the last element from an array using C#
- How to add an element in the last of a JavaScript array?
